Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9152595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:05:34+00:00 2026-06-17T12:05:34+00:00

I have a class that reads a file of a particular format. These files

  • 0

I have a class that reads a file of a particular format. These files tend to be greater than 8Gb in size so are usually compressed. When reading the file in I wanted to catch the error of the file not being compressed but neither except IOError: nor except: will do so, for some reason I don’t understand.

There are a few classes defined together in the file VCF.py, though the offending class is vcfReader(). The file from which the object is instantiated is below test.py, and lastly the Traceback.

Anyone have any ideas as to why it isn’t working?

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)

and then 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)

Here is the traceback:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:05:35+00:00Added an answer on June 17, 2026 at 12:05 pm

    The reason your except block doesn’t catch the exception is that it happens outside the 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large-ish project that contains one class that reads from a file
I have a function that reads lines from a log file, converts these lines
Problem in words: For my application, I have a class that reads from a
I have a words.txt file that I have put in a particular java package
I have a program that reads and draws an SVG file in a java
I have a class that represents some file data. The file contains some headers.
I have a File class that has an Open() method. I have a subclass
I have a multi-R/W lock class that keeps the read, write and pending read
I am new to Java. I just read that class variables in Java have
I have class that extend FragmentActivity in it I add fragment to layout as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.