首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gzip问题、回溯和IOError:[Errno 2]没有这样的文件或目录

Gzip问题、回溯和IOError:[Errno 2]没有这样的文件或目录
EN

Stack Overflow用户
提问于 2011-06-26 06:50:59
回答 3查看 4.1K关注 0票数 2

我是python和生物信息学领域的新手。我使用的是python-2.6。现在我尝试选择所有的fastq.gz文件,然后选择gzip.open(只有几行,因为它太大和浪费时间),然后计算'J‘,然后挑选出那些'J’计数不等于0的文件。

以下是我的代码:

代码语言:javascript
复制
#!/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

但是我得到了一些错误:

代码语言:javascript
复制
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文件,并且确实存在。

希望能给我一些线索,并指出脚本中的任何其他错误。

非常感谢。

编辑: thx everyone我听从了丹的建议。我首先尝试了一个fastq文件。我的脚本是这样的:

代码语言:javascript
复制
#!/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'

那么错误是:

代码语言:javascript
复制
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'

再次编辑: Thx Dan,我昨天已经解决了这个问题。似乎GzipFiles不支持xreadline。因此,我尝试了您稍后建议的类似方法。而且它是有效的。如下所示:

代码语言:javascript
复制
#!/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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-26 07:07:31

在这一行上:

代码语言:javascript
复制
text = gzip.open(file,'r').read()

file是一个文件名,而不是完整路径,因此

代码语言:javascript
复制
fullpath = os.path.join(path, file)
text = gzip.open(fullpath,'r').read()

关于F.readlines()[:10]将把整个文件读入到一个行列表中,然后读取前10行

代码语言:javascript
复制
import itertools
list(itertools.islice(F.xreadlines(),10))

这不会将整个文件读取到内存中,而只会将前10行读取到列表中

但是,当gzip.open返回一个没有.xreadlines()的对象,并且文件在它们的行上是可迭代的时,只需:

代码语言:javascript
复制
list(itertools.islice(F,10))

如本测试所示:

代码语言:javascript
复制
>>> 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']
票数 4
EN

Stack Overflow用户

发布于 2011-06-26 07:19:44

将您的代码更改为:

代码语言:javascript
复制
#!/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
票数 1
EN

Stack Overflow用户

发布于 2011-06-26 07:08:50

它试图从工作目录打开ERR001268_1.recal.fastq.gz,而不是从/home/XXX/nearline

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6481001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档