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

发布于 2024-08-06 01:15:17 字数 375 浏览 3 评论 0原文

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

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

如果我在 7-zip 中打开 gz 文件,我会看到一个与我写入 gz 的路径相匹配的文件夹层次结构我的内容嵌套在多个文件夹深处,例如上面示例中的 /home/joe 或 C: ->文档和设置 ->在 Windows 中等。

我怎样才能得到我正在压缩的一个文件,使其位于 gz 文件的根目录中?

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 here"
f = gzip.open('/home/joe/file.txt.gz', 'wb')
f.write(content)
f.close()

If I open the gz file in 7-zip, I see a folder hierarchy matching the path I wrote the gz to and my content is nested several folders deep, like /home/joe in the example above, or C: -> Documents and Settings -> etc in Windows.

How can I get the one file that I'm zipping to just be in the root of the gz file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放低过去 2024-08-13 01:15:17

看起来您必须直接使用 GzipFile

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

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

It looks like you will have to use GzipFile directly:

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

It looks like open doesn't allow you to specify the fileobj separate from the filename.

随心而道 2024-08-13 01:15:17

您必须使用 gzip.GzipFile 并提供 文件对象。如果这样做,您可以为 gz 文件的标头指定任意文件名。

You must use gzip.GzipFile and supply a fileobj. If you do that, you can specify an arbitrary filename for the header of the gz file.

剩一世无双 2024-08-13 01:15:17

为什么不直接打开文件而不指定目录层次结构(只需执行 gzip.open("file.txt.gz") )?在我看来这很有效。如果需要,您可以随时将文件复制到其他位置。

Why not just open the file without specifying a directory hierarchy (just do gzip.open("file.txt.gz"))?. Seems to me like that works. You can always copy the file to another location, if you need to.

渔村楼浪 2024-08-13 01:15:17

如果将当前工作目录设置为输出文件夹,则可以调用 gzip.open("file.txt.gz") 并且将创建没有层次结构的 gz 文件

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文