我正在尝试制作tokanizer,我正在尝试使用gzip读取一个文件。但它给出了以下错误:
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'# ')
这是我的代码,我才刚刚开始,但是如果python无法读取文件,那我就不会走了。
import gzip
import sys
import re
def main():
file = sys.argv[0]
with gzip.open(file, 'rt') as f:
file_content = f.read()
main()
该文件是.txt.gz文件
答案 0 :(得分:4)
您应该尝试有史以来最简单的调试技术:打印您要使用的值。
无论如何,如果您发现sys.argv[0]
不是您在运行代码的命令后放在命令行上的filename参数,即sys.argv[1]
所以改变:
file = sys.argv[0]
收件人:
file = sys.argv[1]
print( “Reading from file”,file )