python: tarfile extraction error IOError: [Errno 22] invalid mode ('wb') or filename
python: tarfile extraction error IOError: [Errno 22] invalid mode ('wb') or filename
我正在使用 tarfile 提取文件。不幸的是,这个压缩文件来自 linux 服务器,并且包含多个包含非法 Windows OS 字符的文件(':')。
我正在使用以下内容:
extract = tarfile.open(file)
extract.extractall(path=new_path)
extract.close()
我收到以下错误:
IOError: [Errno 22] 无效模式 ('wb') 或文件名: ... "file::ext"
所以我尝试通过以下方式传递错误:
try:
extract = tarfile.open(file)
extract.extractall(path=new_path)
extract.close()
except IOError:
pass
这确实有效,但提取不会继续。它只是因这次失败而停止。
当我用 WinRAR 解压缩文件时,文件自动重命名为 "file__ext"。
是否有 python 的 WinRAR 扩展?或者也许是一种跳过错误并继续提取的方法?或者像 WinRAR 一样自动重命名文件。我不介意文件是否会被跳过。
我看到好几篇帖子都有这个错误,不过都是压缩的,不是解压的。
如果主要目标是批处理这些作业,您可以从命令行调用 winRAR:
import subprocess
subprocess.call(['winRAR.exe', 'x', 'file.rar', 'PathToExtractTo'], shell=True)
我没有测试过上面的代码,但希望它能提供一些想法。
extract = tarfile.open(file)
for f in extract:
# add other unsavory characters in the brackets
f.name = re.sub(r'[:]', '_', f.name)
extract.extractall(path=new_path)
extract.close()
(更改不会保存到原始文件 b/c 我们默认以读取模式打开它。)
我正在使用 tarfile 提取文件。不幸的是,这个压缩文件来自 linux 服务器,并且包含多个包含非法 Windows OS 字符的文件(':')。
我正在使用以下内容:
extract = tarfile.open(file)
extract.extractall(path=new_path)
extract.close()
我收到以下错误: IOError: [Errno 22] 无效模式 ('wb') 或文件名: ... "file::ext"
所以我尝试通过以下方式传递错误:
try:
extract = tarfile.open(file)
extract.extractall(path=new_path)
extract.close()
except IOError:
pass
这确实有效,但提取不会继续。它只是因这次失败而停止。
当我用 WinRAR 解压缩文件时,文件自动重命名为 "file__ext"。
是否有 python 的 WinRAR 扩展?或者也许是一种跳过错误并继续提取的方法?或者像 WinRAR 一样自动重命名文件。我不介意文件是否会被跳过。
我看到好几篇帖子都有这个错误,不过都是压缩的,不是解压的。
如果主要目标是批处理这些作业,您可以从命令行调用 winRAR:
import subprocess
subprocess.call(['winRAR.exe', 'x', 'file.rar', 'PathToExtractTo'], shell=True)
我没有测试过上面的代码,但希望它能提供一些想法。
extract = tarfile.open(file)
for f in extract:
# add other unsavory characters in the brackets
f.name = re.sub(r'[:]', '_', f.name)
extract.extractall(path=new_path)
extract.close()
(更改不会保存到原始文件 b/c 我们默认以读取模式打开它。)