压缩单个文件时的Python gzip文件夹结构

Fog*_*ird 4 python directory gzip

我正在使用Python的gzip模块为单个文件gzip内容,使用类似于文档中的示例的代码:

import gzip
content = "Lots of content here"
f = gzip.open('/home/joe/file.txt.gz', 'wb')
f.write(content)
f.close()
Run Code Online (Sandbox Code Playgroud)

如果我用7-zip打开gz文件,我看到一个文件夹层次结构与我编写gz的路径相匹配,我的内容嵌套了几个文件夹,如上例中的/ home/joe,或C: - > Documents and Windows中的设置 - >等.

如何才能将我正在压缩的文件放在gz文件的根目录中?

Kat*_*one 10

看起来你必须GzipFile直接使用:

import gzip
content = "Lots of content here"
real_f = open('/home/joe/file.txt.gz', 'wb')
f = gzip.GZipFile('file.txt.gz', fileobj = realf)
f.write(content)
f.close()
real_f.close()
Run Code Online (Sandbox Code Playgroud)

看起来open似乎不允许您指定与文件名分开的fileobj.