尝试打开现有文件时发生IOError [英] IOError when trying to open existing files
问题描述
合并到500列表,然后使用生成的合并列表。循环(下面的代码)需要执行我的功能
extract_zcoords()超过500个文件(1个文件给出一个列表),以便我可以建立一个数据集。
-----代码
#!/ usr / local / bin
import sys,string $ b $ from sys import version
#********
def extract_zcoord(filename):
'''收集带电荷氨基酸原子的z坐标'''
f = open(filename,'r')
pdbfile = f.readlines()
# ##代码,处理文件和构建z_coords1列表
### ....
在范围内(len(charged_groups)):
z_coords1.append(float( charge_groups [i] [48:54]))
print z_coords1 #diagnostic
return z_coords1
#********
### LOOP OVER DIRECTORY
location ='/ Users / spyros / Desktop / 3NY8MODELSHUMAN / HomologyModels'
zdata = []
for os.listdir(location):
try:
zdata.extend(extract_zcoord(filename))
除了NameError:
printNo such文件!
,除了SyntaxError:
print检查你的语法!
除IOError:
打印PDB文件未找到!
print'Z-VALUES FOR ALL CHARGED RESIDUES'
print zdata #diagnostic
import pickle
f1 = open(z_coords1.dat,wb)
pickle.dump(zdata,f1)
f1.close()
f2 = open(z_coords1.dat ,rb)
zdata1 = pickle.load(f2)
f2.close()
assert zdata == zdata1,pickle / unpickle round trip中的错误!
错误:
IOException是发生的那个,所以出于某种原因
python不能访问文件。我已经检查了目录路径名(位置)和
文件的权限,它们是正确的(读取+写入)。任何想法,为什么一个IOError将
报告,当文件存在和路径名是正确的?我试过os.pathisfile(位置),它回来了FALSE 。我不太清楚
该做什么,因为当我尝试在os.listdir中为文件名添加(b
位置):
打印文件名
我得到所有打印在屏幕上的文件名!任何想法可能是错的?
位置='/ Users / spyros / Desktop / 3NY8MODELSHUMAN / HomologyModels'
用于os.listdir(位置)中的文件名:
filename = os.path。连接(位置,文件名)
I have a small issue with a python program that I wrote to extract some information from a special text file. What I'm trying to do is merge together ~500 lists and then use the resulting merged list. The loop (code below) needs to execute my function extract_zcoords() over 500 files (1 file gives one list) so that I can build a dataset.
-----THE CODE
#!/usr/local/bin
import os,string
from sys import version
# ********
def extract_zcoord(filename):
''' collect z-coordinates of atoms from charged amino acids '''
f = open(filename, 'r')
pdbfile = f.readlines()
### Code that processes file and builds z_coords1 list
### ....
for i in range(len(charged_groups)):
z_coords1.append(float(charged_groups[i][48:54]))
print z_coords1 #diagnostic
return z_coords1
# ********
### LOOP OVER DIRECTORY
location = '/Users/spyros/Desktop/3NY8MODELSHUMAN/HomologyModels'
zdata = []
for filename in os.listdir(location):
try:
zdata.extend(extract_zcoord(filename))
except NameError:
print "No such file!"
except SyntaxError:
print "Check Your Syntax!"
except IOError:
print "PDB file NOT FOUND!"
print 'Z-VALUES FOR ALL CHARGED RESIDUES'
print zdata #diagnostic
import pickle
f1 = open("z_coords1.dat", "wb")
pickle.dump(zdata, f1)
f1.close()
f2 = open("z_coords1.dat", "rb")
zdata1 = pickle.load(f2)
f2.close()
assert zdata == zdata1, "error in pickle/unpickle round trip!"
THE ERROR: The IOException is the one that occurs, so for some reason python is not accessing the files. I have checked directory pathname (location) and file permissions, and they are correct (read+write). Any ideas why an IOError would be reported when the files do exist and pathname is correct?
I tried os.pathisfile(location) and it comes back FALSE. I am not quite sure what to make of that, since when I try
for filename in os.listdir(location):
print filename
I get all files names printed on screen! Any ideas what might be wrong?
You need to join the dirname and filename into one complete path:
location = '/Users/spyros/Desktop/3NY8MODELSHUMAN/HomologyModels'
for filename in os.listdir(location):
filename = os.path.join(location, filename)
这篇关于尝试打开现有文件时发生IOError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!