簡體   English   中英

python無法使用gzip讀取gzip文件

[英]python is not reading a gzip file with gzip

我正在嘗試制作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文件

您應該嘗試最簡單的調試技術:打印您要使用的值。

無論如何,如果這樣做,您會看到sys.argv[0]不是您在運行代碼的命令后在命令行上輸入的filename參數-即sys.argv[1]

所以改變:

file = sys.argv[0]

至:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM