gpt4 book ai didi

python - 尝试除了不从类中捕获 IOError

转载 作者:行者123 更新时间:2023-11-28 22:57:47 28 4
gpt4 key购买 nike

我有一个类可以读取特定格式的文件。这些文件的大小往往大于 8Gb,因此通常会进行压缩。在读取文件时,我想捕获文件未被压缩的错误,但 except IOError:except: 都不会这样做,出于某种原因我不会这样做明白了。

在文件 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

最佳答案

您的 except block 没有捕获异常的原因是它发生在 try block 之外:

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

关于python - 尝试除了不从类中捕获 IOError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14417474/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com