简体   繁体   中英

python is not reading a gzip file with gzip

I'm trying to make a tokanizer, I have a file that I'm trying to read with gzip. but it gives the following error:

Traceback (most recent call last):
  File "extract_sends.py", line 14, in <module>
    main()
  File "extract_sends.py", line 12, in main
    file_content = f.read()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/gzip.py", line 276, in read
    return self._buffer.read(size)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/gzip.py", line 463, in read
    if not self._read_gzip_header():
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/gzip.py", line 411, in _read_gzip_header
    raise OSError('Not a gzipped file (%r)' % magic)
OSError: Not a gzipped file (b'# ')

This is my code, I'm just starting but if python can't read the file I'm not comming far.

import gzip
import sys
import re

def main():
    file = sys.argv[0]
    with gzip.open(file, 'rt') as f:
        file_content = f.read()

main()

The file is a .txt.gz file

You should try the simplest ever debugging technique: print the value you are trying to use.

Anyway if you did that you would see that sys.argv[0] is not the filename parameter you put on the commandline after the command to run your code - that is sys.argv[1]

So change:

file = sys.argv[0]

To:

file = sys.argv[1]
print( “Reading from file”,file )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM