Python gzip folder structure when zipping single file

前端 未结 4 1653

I\'m using Python\'s gzip module to gzip content for a single file, using code similar to the example in the docs:

import gzip
content = \"Lots of content he         


        
4条回答
  •  渐次进展
    2021-01-13 17:18

    If you set your current working directory to your output folder, you can then call gzip.open("file.txt.gz") and the gz file will be created without the hierarchy

    import os
    import gzip
    content = "Lots of content here"
    outputPath = '/home/joe/file.txt.gz'
    origDir = os.getcwd()
    os.chdir(os.path.dirname(outputPath))
    f = gzip.open(os.path.basename(outputPath), 'wb')
    f.write(content)
    f.close()
    os.chdir(origDir)
    

提交回复
热议问题