2013-01-19 73 views
2

我有一個類讀取特定格式的文件。這些文件的大小通常大於8Gb,因此通常會進行壓縮。在讀取文件時,我想捕捉文件未被壓縮的錯誤,但except IOError:except:都不會這樣做,出於某種原因,我不明白。嘗試除了沒有捕獲類的IOError

在文件VCF.py中有幾個類一起定義,儘管違規類是vcfReader()。對象實例化的文件在test.py以下,最後是Traceback。

任何人有任何想法,爲什麼它不工作?

VCF.py

import gzip 
import sys 

class Call(): 
    ''' 
    Class to handle the sample genotypes and associated information 
    ''' 

    def __init__(self,site,sample,format,data): 
     #do stuff here# 

class Variant(): 
    ''' 
    Class for a single row from a VCF file. 
    ''' 
    def __init__(self, entry, samples): 
     #do other stuff here 


class vcfReader(): 
    ''' 
    read a compressed vcf file ignoring the meta-information, but parsing the header    for sample names 
    ''' 
    def __init__(self, file): 
     try: 
      self.vcfFile = gzip.open(file, 'rb') 
     except IOError: 
      print "Not a gzipped file" 
      sys.exit() 

     self.samples = self.readHeader() 

    def readHeader(self): 
     line = self.vcfFile.next() 
     while line.startswith('#'): 
      if line[1]!='#': 
       #lines that start with ##, i.e. meta tags are ignored. Header line starting with '#', sample names are extracted. 
       return line.rstrip().rsplit('\t')[9:] 
      else:   
       line = self.vcfFile.next() 

    def __iter__(self): 
     return self 

    def next(self): 
     row = self.vcfFile.next() 
     return Variant(row, self.samples) 

然後test.py

import VCF 
from collections import Counter 

if __name__=='__main__': 
    vcfreader = VCF.vcfReader('all_samples.vcf') 

    filters = [] 
    for i in vcfreader: 
     filters.extend(i.FILTERS) 

    filters = Counter(filters) 

    for k,v in filters.iteritems(): 
     print "{0}: {1}".format(k,v) 

這裏是回溯:

Traceback (most recent call last): 
    File "C:\Users\Davy\Documents\Programming\VCF_stuff\src\test.py", line 10, in <module> 
    vcfreader = VCF.vcfReader('all_samples.vcf') 
    File "C:\Users\Davy\Documents\Programming\VCF_stuff\src\VCF.py", line 95, in __init__ 
    self.samples = self.readHeader() 
    File "C:\Users\Davy\Documents\Programming\VCF_stuff\src\VCF.py", line 98, in readHeader 
    line = self.vcfFile.next() 
    File "C:\Python27\lib\gzip.py", line 450, in readline 
    c = self.read(readsize) 
    File "C:\Python27\lib\gzip.py", line 256, in read 
    self._read(readsize) 
    File "C:\Python27\lib\gzip.py", line 291, in _read 
    self._read_gzip_header() 
    File "C:\Python27\lib\gzip.py", line 185, in _read_gzip_header 
    raise IOError, 'Not a gzipped file' 
IOError: Not a gzipped file 

回答

4

的原因,你的except塊不趕上例外的是,它發生在try塊之外:

def __init__(self, file): 
    try: 
     self.vcfFile = gzip.open(file, 'rb') 
    except IOError: 
     print "Not a gzipped file" 
     sys.exit() 

    self.samples = self.readHeader() # <<<<<<<< exception is raised here 
+1

'如果你試圖打開一個非gzip文件gzip.open'不會拋出一個錯誤,因爲你只能通過嘗試解析檢測這樣的嘗試該文件作爲gzip文件並獲取解​​析錯誤。這不是簡單地通過打開文件就會發生的事情。 – chepner