gzip a file in Python

后端 未结 7 2054
悲&欢浪女
悲&欢浪女 2020-12-28 12:47

I want to gzip a file in Python. I am trying to use the subprocss.check_call(), but it keeps failing with the error \'OSError: [Errno 2] No such file or directory\'. Is ther

相关标签:
7条回答
  • 2020-12-28 13:12

    Use the gzip module:

    import gzip
    import os
    
    in_file = "somefile.data"
    in_data = open(in_file, "rb").read()
    out_gz = "foo.gz"
    gzf = gzip.open(out_gz, "wb")
    gzf.write(in_data)
    gzf.close()
    
    # If you want to delete the original file after the gzip is done:
    os.unlink(in_file)
    

    Your error: OSError: [Errno 2] No such file or directory' is telling you that the file fullFilePath does not exist. If you still need to go that route, please make sure that file exists on your system and you are using an absolute path not relative.

    0 讨论(0)
提交回复
热议问题