python - 从自定义路径中提取 .ppm.bz2 到自定义路径
问题描述
正如标题所说,我有几个文件夹,几个 .ppm.bz2 文件,我想将它们准确地提取到它们使用 python 的位置。
我在文件夹中遍历是这样的:
import tarfile
import os
path = '/Users/ankitkumar/Downloads/colorferet/dvd1/data/images/'
folders = os.listdir(path)
for folder in folders: #the folders starting like 00001
if not folder.startswith("0"):
pass
path2 = path + folder
zips = os.listdir(path2)
for zip in zips:
if not zip.startswith("0"):
pass
path3 = path2+"/"+zip
fh = tarfile.open(path3, 'r:bz2')
outpath = path2+"/"
fh.extractall(outpath)
fh.close
`
然后我得到这个错误`
Traceback (most recent call last):
File "ZIP.py", line 16, in <module>
fh = tarfile.open(path3, 'r:bz2')
File "/anaconda2/lib/python2.7/tarfile.py", line 1693, in open
return func(name, filemode, fileobj, **kwargs)
File "/anaconda2/lib/python2.7/tarfile.py", line 1778, in bz2open
t = cls.taropen(name, mode, fileobj, **kwargs)
File "/anaconda2/lib/python2.7/tarfile.py", line 1723, in taropen
return cls(name, mode, fileobj, **kwargs)
File "/anaconda2/lib/python2.7/tarfile.py", line 1587, in __init__
self.firstmember = self.next()
File "/anaconda2/lib/python2.7/tarfile.py", line 2370, in next
raise ReadError(str(e))
tarfile.ReadError: invalid header
`
解决方案
tarfile 模块用于 tar 文件,包括tar.bz2
. 如果你的文件不是tar
你应该bz2
直接使用模块。
另外,尝试使用os.walk
而不是多个listdir
,因为它可以遍历树
import os
import bz2
import shutil
for path, dirs, files in os.walk(path):
for filename in files:
basename, ext = os.path.splitext(filename)
if ext.lower() != '.bz2':
continue
fullname = os.path.join(path, filename)
newname = os.path.join(path, basename)
with bz2.open(fullname) as fh, open(newname, 'wb') as fw:
shutil.copyfileobj(fh, fw)
这将解压缩所有.bz2
子文件夹中的所有文件,在它们所在的位置。所有其他文件将保持不变。如果未压缩的文件已经存在,它将被覆盖。
请在运行破坏性代码之前备份您的数据
推荐阅读
- r - 如何训练具有大量分类特征的模型 :: RStudio 崩溃
- javascript - 请求方法未显示在 this.props 中
- python - Pytest 在尝试运行测试时给我一个错误
- numpy - 将 numpy.datetime64 转换为纪元时间
- r - 在 r 中的数据表中找到前 1 个百分位数
- c++ - 检查数组中的所有对是否都可以被 k 整除
- html - 如何在componentDidMount(使用react.js)中将div滚动到底部?
- deep-linking - 如何将路由与 Branch.io 后备 utl 一起使用
- python-3.x - 如何使用 Python/Selenium 在 Google 地图中输入值
- java - vscode 无法解析在 Maven 配置文件中声明的依赖项