Python gzip refuses to read uncompressed file

前端 未结 4 1692
一整个雨季
一整个雨季 2021-01-17 13:31

I seem to remember that the Python gzip module previously allowed you to read non-gzipped files transparently. This was really useful, as it allowed to read an input file wh

4条回答
  •  死守一世寂寞
    2021-01-17 14:22

    Maybe you're thinking of zless or zgrep, which will open compressed or uncompressed files without complaining.

    Can you trust that the file name ends in .gz?

    if file_name.endswith('.gz'):
        opener = gzip.open
    else:
        opener = open
    
    with opener(file_name, 'r') as f:
        ...
    

提交回复
热议问题