Search code examples
pythonpython-3.xmacosgzip

Error when using gzip on a file containing line breaks


I'm attempting to use python's gzip library to streamline some python scripts that create csv output files. I've tried a number of different methods of creating the gzip file, but no matter which method I've tried, I'm running into the same issue.

My python script runs successfully, but when I try to decompress the gzip file in Finder (using MacOS 10.15.6), I'm prompted with the following error:

Unable to expand "file.csv.gz" into "Documents". (Error 79 - Inappropriate file type or format.)

After some debugging, I've narrowed down the cause of the error to the file content containing line break (\n) characters.

This simple example code triggers the above error on gzip expansion:

import gzip

content = b'Id,Food\n1,Spam\n2,Eggs\n'
f = gzip.open('file.csv.gz', 'wb')
f.write(content)
f.close()

When I remove all \n characters from the content variable, everything works fine:

import gzip

content = b'Id,Food,1,Spam,2,Eggs'
f = gzip.open('file.csv.gz', 'wb')
f.write(content)
f.close()

Does gzip want me to use a different line break mechanism? I'm sure I'm missing some sort of foundational knowledge about gzip or binaries, so any info that helps get me back on track would be much appreciated.


Solution

  • It has nothing to do with Python's gzip. It is, arguably, a bug in macOS where it sometimes detects the resulting uncompressed data as an mtree by the Archive Utility, but then finds the uncompressed data violates the mtree format.

    The solution is to not double-click to decompress. Use gzip to decompress.