问题标签 [tarfile]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
5 回答
71999 浏览

python - 打开文件对象的大小

有没有办法找到当前打开的文件对象的大小?

具体来说,我正在使用 tarfile 模块来创建 tarfile,但我不希望我的 tarfile 超过一定的大小。据我所知,tarfile 对象是类似文件的对象,所以我想一个通用的解决方案会起作用。

0 投票
4 回答
10424 浏览

python - 如何确定数据是否是没有文件的有效 tar 文件?

我的上传表单需要一个 tar 文件,我想检查上传的数据是否有效。tarfile模块支持,is_tarfile()但需要一个文件名——我不想浪费资源将文件写入磁盘只是为了检查它是否有效。

有没有办法使用标准 Python 库在不写入磁盘的情况下检查数据是否为有效的 tar 文件?

0 投票
1 回答
1023 浏览

python - 为什么 tarfile.extractall 默认会忽略错误?

Python 的tarfile模块默认在提取过程中忽略错误,除非errorlevel设置为1or 2(或者如果只需要打印错误消息)debug1

尝试使用 amkdir /tmp/foo && sudo chown root /tmp/foo && chmod a-w /tmp/foo和 usingtarfile来提取一个 .tar.gz 文件/tmp/foo——你会看到你的 Python 代码根本没有抛出异常。文件不会被提取/tmp/foo,仍然是一个空目录。

为什么会有这种行为?谁/什么从这种默认行为中受益?换句话说,在解压缩 tar 文件时,谁/什么想要忽略权限错误?

0 投票
8 回答
141243 浏览

python - 如何使用 Python 创建完整的压缩 tar 文件?

如何在 Python 中创建带有压缩的 .tar.gz 文件?

0 投票
3 回答
245 浏览

python - tarfile:确定打开的 tarball 的压缩

我正在研究一个 Python 脚本,该脚本应该处理一个 tarball 并输出新的,试图保持原始格式。因此,我正在寻找一种方法来查找打开的压缩包中使用的压缩方法,以打开具有相同压缩的新压缩包。

AFAICSTarFile类不提供任何公共接口来直接获取所需信息。而且我想避免独立于 tarfile 模块读取文件。

我目前正在考虑查找底层文件对象 ( t.fileobj.__class__) 的类或尝试以所有可能的模式打开输入文件并根据成功的模式选择正确的格式。

0 投票
3 回答
2563 浏览

python - 获取python tarfile以跳过没有读取权限的文件

我正在尝试编写一个函数,该函数将具有不同权限文件的目录备份到 Windows XP 上的存档。我正在使用 tarfile 模块来 tar 目录。目前,一旦程序遇到没有读取权限的文件,它就会停止给出错误:IOError: [Errno 13] Permission denied: 'path to file'。我希望它只是跳过它无法读取的文件,而不是结束 tar 操作。这是我现在使用的代码:

0 投票
2 回答
959 浏览

python - Organizing files in tar bz2 file with python

I have about 200,000 text files that are placed in a bz2 file. The issue I have is that when I scan the bz2 file to extract the data I need, it goes extremely slow. It has to look through the entire bz2 file to fine the single file I am looking for. Is there anyway to speed this up?

Also, I thought about possibly organizing the files in the tar.bz2 so I can instead have it know where to look. Is there anyway to organize files that are put into a bz2?

More Info/Edit: I need to query the compressed file for each textfile. Is there a better compression method that supports such a large number of files and is as thoroughly compressed?

0 投票
2 回答
1320 浏览

python - Python tarfile 模块在提取过程中覆盖现有文件 - 如何禁用它?

有没有办法防止tarfile.extractallAPI)覆盖现有文件?“防止”是指理想情​​况下在即将发生覆盖时引发异常。当前行为是静默覆盖文件。

0 投票
4 回答
4271 浏览

python - 如何使用 Python 检查文件是否存在于 tar 存档中?

我想在将给定文件作为类文件对象获取之前,使用 Python 验证 tar 存档中是否存在给定文件。我已经尝试过isreg(),但可能我做错了什么。

如何使用 Python 检查文件是否存在于 tar 存档中?

我试过了

大概是错的。事实上tai.size()总是0。

0 投票
1 回答
954 浏览

python - Python 的 `tarfile` 模块是否将它正在构建的档案存储在内存中?

我在内存受限的环境中工作,我需要制作 SQL 转储存档。如果我使用 python 的内置tarfile模块,是在内存中保存的“.tar”文件还是在创建时写入磁盘?

例如,在下面的代码中,如果huge_file.sql是 2GB,tar变量会占用 2GB 的内存吗?