我试图将数据从fast5文件写入txt文件。我可以通过进入文件所在的目录并使用以下代码来完成此操作:
for filename in os.listdir(os.getcwd()):
if filename.endswith('.fast5'):
with h5py.File(filename, 'r') as hdf:
with open(new_txt, 'a') as myfile:
myfile.write('%s \t' % (filename))
但是,我现在尝试通过主目录访问文件,循环访问文件所在的特定子文件夹并使用以下代码访问文件:
for root, dirs, files in os.walk(path):
for d in dirs:
if d.startswith('pass') or d.startswith('fail')
for rootfolder, blankdirs, fast5files in os.walk(d):
for filename in fast5files:
if filename.endswith('.fast5'):
with h5py.File(filename, 'r') as hdf:
with open(new_txt, 'a') as myfile:
myfile.write('%s \t' % (filename))
此代码给出错误:
IOError: Unable to open file (Unable to open file: name = 'minion2_chip61_re_n90_yt2_2644_1_ch108_file0_strand.fast5', errno = 2, error message = 'no such file or directory', flags = 0, o_flags = 0)
让我感到困惑,因为它能够获取文件名,但不知何故无法读取它,它可以在原始代码下。错误发生在这一行:
with h5py.File(filename, 'r') as hdf:
为什么h5py无法以这种方式打开/读取文件?
答案 0 :(得分:1)
您需要添加当前正在遍历文件名的目录os.walk
:
....
if filename.endswith('.fast5'):
hdf5_path = os.path.join(root, filename)
with h5py.File(hdf5_path, 'r') as hdf:
...