reading from an a gzip file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nader

    reading from an a gzip file

    Hello,

    I have a gzip file and I try to read from this file withe the next
    statements:

    gunziped_file = gzip.GzipFile(' gzip-file')
    input_file = open(gunziped_f ile,'r')

    But I get the nezt error message:

    Traceback (most recent call last):
    File "read_sfloc_fil es.py", line 131, in ?
    input_file = open(gunziped_f ile,'r')
    TypeError: coercing to Unicode: need string or buffer, instance found

    I think that I do some mistake. Would some body tell me what is my
    mistake?

    Nader
  • Matt Nordhoff

    #2
    Re: reading from an a gzip file

    Nader wrote:
    Hello,
    >
    I have a gzip file and I try to read from this file withe the next
    statements:
    >
    gunziped_file = gzip.GzipFile(' gzip-file')
    input_file = open(gunziped_f ile,'r')
    >
    But I get the nezt error message:
    >
    Traceback (most recent call last):
    File "read_sfloc_fil es.py", line 131, in ?
    input_file = open(gunziped_f ile,'r')
    TypeError: coercing to Unicode: need string or buffer, instance found
    >
    I think that I do some mistake. Would some body tell me what is my
    mistake?
    >
    Nader
    gzip.GzipFile() creates a file-like object. You don't call open() on it;
    you use it directly.

    $ echo foo >test
    $ gzip test
    $ python
    >>import gzip
    >>gfh = gzip.GzipFile(' test.gz', 'rb')
    >>gfh.read()
    'foo\n'
    >>gfh.close()
    --

    Comment

    Working...