Reputation: 81
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
Upvotes: 1
Views: 5044
Reputation: 6826
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 )
Upvotes: 2