Gzip问题,回溯错误及IOError: [Errno 2] 没有这样的文件或目录
我刚接触Python和生物信息学,正在使用Python 2.6。现在我想选择所有的fastq.gz文件,然后用gzip.open(因为文件太大,处理起来费时,所以只写了几行代码),接着统计其中'J'的数量,最后挑选出那些'J'的数量不等于0的文件。
以下是我的代码:
#!/usr/bin/python
import os,sys,re,gzip
path = "/home/XXX/nearline"
for file in os.listdir(path):
if re.match('.*\.recal.fastq.gz', file):
text = gzip.open(file,'r').readlines()[:10]
word_list = text.split()
number = word_list.count('J') + 1
if number !== 0:
print file
但是我遇到了一些错误:
Traceback (most recent call last):
File "fastqfilter.py", line 9, in <module>
text = gzip.open(file,'r').readlines()[:10]
File "/share/lib/python2.6/gzip.py", line 33, in open
return GzipFile(filename, mode, compresslevel)
File "/share/lib/python2.6/gzip.py", line 79, in __init__
fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
IOError: [Errno 2] No such file or directory: 'ERR001268_1.recal.fastq.gz'
这个错误信息是什么意思:文件......
gzip这里有什么问题吗?为什么找不到ERR001268_1.recal.fastq.gz?它在列表中是第一个fastq文件,而且确实存在。
希望能给我一些线索,也请指出脚本中的其他错误。
非常感谢。
编辑:谢谢大家。我按照Dan的建议,先尝试了一个fastq文件。我的脚本如下:
#!/usr/bin/python
import os,sys
import gzip
import itertools
file = gzip.open('/home/xug/nearline/ERR001274_1.recal.fastq.gz','r')
list(itertools.islice(file.xreadlines(),10))
word_list = list.split()
number = word_list.count('J') + 1
if number != 0:
print 'ERR001274_1.recal.fastq.gz'
然后出现的错误是:
Traceback (most recent call last):
File "try2.py", line 8, in <module>
list(itertools.islice(text.xreadlines(),10))
AttributeError: GzipFiles instance has no attribute 'xreadlines'
再编辑一次:谢谢Dan,我昨天解决了这个问题。似乎GzipFiles不支持xreadlines。所以我尝试了你后面建议的类似方法,结果成功了。见下:
#!/usr/bin/python
import os,sys,re
import gzip
from itertools import islice
path = "/home/XXXX/nearline"
for file in os.listdir(path):
if re.match('.*\.recal.fastq.gz', file):
fullpath = os.path.join(path, file)
myfile = gzip.open(fullpath,'r')
head = list(islice(myfile,1000))
word_str = ";".join(str(x) for x in head)
number = word_str.count('J')
if number != 0:
print file
3 个回答
0
它试图从当前工作目录打开 ERR001268_1.recal.fastq.gz
文件,而不是从 /home/XXX/nearline
这个路径。
1
把你的代码改成这样:
#!/usr/bin/python
import os,sys,re,gzip
path = "/home/XXX/nearline"
for file in os.listdir(path):
if re.match('.*\.recal.fastq.gz', file):
text = gzip.open(os.path.join(path,file),'r').readlines()[:10]
word_list = text.split()
number = word_list.count('J') + 1
if number !== 0:
print file
4
在这一行:
text = gzip.open(file,'r').read()
file
是一个文件名,而不是完整的路径,所以
fullpath = os.path.join(path, file)
text = gzip.open(fullpath,'r').read()
关于 F.readlines()[:10]
这段代码,它会把整个文件读入一个行的列表中,然后取出前10行
import itertools
list(itertools.islice(F.xreadlines(),10))
这不会把整个文件都读到内存中,只会把前10行读入一个列表
但是因为 gzip.open
返回的对象没有 .xreadlines()
方法,而文件是可以按行遍历的,所以只需:
list(itertools.islice(F,10))
这段代码的测试显示这样是可行的:
>>> import gzip,itertools
>>> list(itertools.islice(gzip.open("/home/dan/Desktop/rp718.ps.gz"),10))
['%!PS-Adobe-2.0\n', '%%Creator: dvips 5.528 Copyright 1986, 1994 Radical Eye Software\n', '%%Title: WLP-94-int.dvi\n', '%%CreationDate: Mon Jan 16 16:24:41 1995\n', '%%Pages: 6\n', '%%PageOrder: Ascend\n', '%%BoundingBox: 0 0 596 842\n', '%%EndComments\n', '%DVIPSCommandLine: dvips -f WLP-94-int.dvi\n', '%DVIPSParameters: dpi=300, comments removed\n']