- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法提取生成的 tar.gz 文件,因为 extractall()
提示目标目录存在。但是,如果提取目录不存在,它只会生成一个空文件。
我在网上找到的有关提取 tar 文件的所有示例 use no parameters对于 tarfile.extractall()
(这意味着它尝试将其提取到同一目录中,但因 IsADirectoryError
而失败)或 make sure to create the extraction path beforehand 。
这里使用的是 Python 3.5.2。
复制脚本:
#!/usr/bin/python3
import os, tarfile, tempfile
# Create a test directory
test_dir = os.path.join(os.path.expanduser('~'), 'tarfile-test')
os.makedirs(test_dir, exist_ok=True)
os.chdir(test_dir)
# Create empty files to include in the tarfile
open('1.txt', 'a').close()
open('2.txt', 'a').close()
open('3.txt', 'a').close()
# Create the tarfile
compressed_file = 'packet.tgz'
with tarfile.open(compressed_file, 'w:gz') as tar:
for f in os.listdir():
tar.add(f, arcname=os.path.sep)
# Now attempt to extract it in three different places: a local directory, a
# temporary directory and a non-existent directory
# Local directory
local_dir = 'local-extraction'
os.makedirs(local_dir, exist_ok=True)
try:
with tarfile.open(compressed_file, 'r:gz') as tar:
tar.extractall(path=local_dir)
print('Extracted in local dir!')
except IsADirectoryError:
print('Failed to extract in local directory')
# Temporary directory
try:
with tempfile.TemporaryDirectory() as tmp_dir:
with tarfile.open(compressed_file, 'r:gz') as tar:
tar.extractall(path=tmp_dir)
print('Extracted in temporary dir!')
except IsADirectoryError:
print('Failed to extract in temporary directory')
# Non-existent directory. This does not throw an exception, but fails to extract
# the files
non_existent = 'non_existent_dir'
with tarfile.open(compressed_file, 'r:gz') as tar:
tar.extractall(path=non_existent)
if os.path.isdir(non_existent):
print('Extracted in previously non-existent dir!')
else:
print('Not extracted in non-existent dir')
输出:
$ ./repro.py
Failed to extract in local directory
Failed to extract in temporary directory
Not extracted in non-existent dir
如果我们检查 tarfile-test
的内容:
$ ll
total 16
drwxrwxr-x 3 user user 4096 Jul 11 08:38 ./
drwxr-xr-x 31 user user 4096 Jul 11 08:38 ../
-rw-rw-r-- 1 user user 0 Jul 11 08:38 1.txt
-rw-rw-r-- 1 user user 0 Jul 11 08:38 2.txt
-rw-rw-r-- 1 user user 0 Jul 11 08:38 3.txt
drwxrwxr-x 2 user user 4096 Jul 11 08:38 local-extraction/
-rw-rw-r-- 1 user user 0 Jul 11 08:38 non_existent_dir
-rw-rw-r-- 1 user user 124 Jul 11 08:38 packet.tgz
non_existent_dir
是一个空文件,而不是目录。 local-extraction
为空。
我错过了什么?
最佳答案
看起来问题出在创建 tar.gz 文件时的 arcname
参数中。我(错误地)遵循了建议in this comment 。但是,这只应在打包目录时执行此操作,它会损坏添加单个文件时使用的 tar.gz 文件。
更改/删除 tarfile.add()
中的 arcname
参数可修复此问题:
# Create the tarfile
compressed_file = 'packet.tgz'
with tarfile.open(compressed_file, 'w:gz') as tar:
for f in os.listdir():
tar.add(f)
关于python - tarfile.extractall() 引发 IsADirectoryError 因为提取路径存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45027246/
using (ZipFile zip = ZipFile.Read("update.zip")) { zip.ExtractAll("\\serv\\www", ExtractExistingF
我正在尝试使用 Java 解压缩此文件。我找到了本指南:What is a good Java library to zip/unzip files?但我收到错误:对于 ZipFile 类型,方法 e
我有一个遗留脚本,它通过 python 脚本获取 boost 库并提取然后构建它们。 在 Windows 上,提取步骤失败,因为 boost 存档中的某些文件的路径太长。例如。 IOError: [E
我有一个嵌套的字符串列表,我想从中提取日期。日期格式为: Two numbers (from 01 to 12) hyphen tree letters (a valid month) hyphen
我有一个问题,我感觉之前可能已经有人问过这个问题,但形式不同。如果是这种情况,请指点我的原文。 无论如何,我正在使用 Pandas extractall() 方法,我不太喜欢它返回一个带有多级索引 (
我正在使用 Zip4J 来提取 zip 文件,并且我能够做到这一点。但是,我想使用 Zip4J 中提供的进度监视器,但无法成功使用它。文档只说它应该在线程模式下运行 true。我做到了,我的控制台在命
我从一个文件中读取了一个数据集,我认为它看起来像这样(总共 500-600 行): 0,['' ''] 1,['Size' 'S'] 2,['Energy (kJ)' '1644'] 3,['Prot
您好,我目前正在开发一个必须提取一些 .tar 文件的工具。 它在大多数情况下都工作得很好,但我有一个问题: 某些 .tar 和 .zip 文件的名称包含“非法”字符(f.ex“:”)。这个程序必须在
Python 的 tarfile模块在提取期间忽略错误默认情况下,除非errorlevel 设置为1 或2(或 debug 到 1 如果只需要打印错误消息)。 尝试执行 mkdir/tmp/foo &
我有一个数据框,我想提取一个模式的所有出现,然后将它们作为单个字符串连接起来,成为我原始数据框的一个新列。 例子: original = pd.DataFrame(["bonjour bonjour"
我尝试使用 zipfile 将文件夹中的所有 zip 文件提取到该文件夹中,但收到 TypeError: TypeError: extractall() missing 1 required po
Python tarfile 库没有检测到损坏的 tar。 user@host$ wc -c good.tar 143360 good.tar user@host$ head -c 130000 go
本文整理了Java中org.openl.util.ZipUtils.extractAll()方法的一些代码示例,展示了ZipUtils.extractAll()的具体用法。这些代码示例主要来源于Git
我无法提取生成的 tar.gz 文件,因为 extractall() 提示目标目录存在。但是,如果提取目录不存在,它只会生成一个空文件。 我在网上找到的有关提取 tar 文件的所有示例 use no
在 python 文档中,建议不要在未事先检查的情况下提取 tar 存档。使用 tarfile python 模块确保存档安全的最佳方法是什么?我是否应该遍历所有文件名并检查它们是否包含绝对路径名?
本文整理了Java中net.lingala.zip4j.core.ZipFile.extractAll()方法的一些代码示例,展示了ZipFile.extractAll()的具体用法。这些代码示例主要
我正在运行 python zipfile extractall,它解压缩到一个超过 255 个字符的路径。在 Windows 7 64 位上运行它。我遇到以下错误 [Errno 2] No such
我正在使用 python tarfile.extractall() 来解压 foo.tar.gz 文件。我想访问提取的文件夹,但有时提取的文件夹的名称与打包文件的名称不同。我需要一种方法来控制提取的文
我正在尝试从推文文本中过滤所有 # 关键字。我正在使用 str.extractall() 提取所有带有 # 关键字的关键字。这是我第一次使用 pandas 从 tweetText 中过滤关键字。下面给
我正在使用 Embarcadero C++Builder 10.1 Berlin Update 2。我正在使用 System.Zip.TZipFile.ExtractAll() 来提取一个大的 .zi
我是一名优秀的程序员,十分优秀!