我有一段代码用于提取过程。首先,我为zip文件做了它,但我发现我也有rar文件。因此,我安装了rarfile库并实现了提取过程。
但是,代码似乎会引发异常,因为扫描的第一批文件是.zip文件。这就解释了,我想,为什么我会有这个错误:
raise NotRarFile("Not a Rar archive: "+self.rarfile)
NotRarFile: Not a Rar archive: /Users/me/Downloads/_zips/test2/Break_The_Bans_-_Covers__B-sides.zip提取代码如下:
for ArchivesFiles in chemin_zipfiles :
truncated_file = os.path.splitext(os.path.basename(ArchivesFiles))[0]
if not os.path.exists(truncated_file):
os.makedirs(truncated_file)
rar_ref = rarfile.RarFile(ArchivesFiles,'r')
zip_ref = zipfile.ZipFile(ArchivesFiles,'r')
new_folder = os.path.realpath(truncated_file)
rar_ref.extractall(new_folder)
zip_ref.extractall(new_folder)在调用此代码之前,我将检索所有具有.zip和.rar扩展名的文件:
chemin_zipfiles = [os.path.join(root, name)
for root, dirs, files in os.walk(directory)
for name in files
if name.endswith((".zip", ".rar"))]那么,我如何在相同的过程和功能中解压缩和解压缩?我哪里错了?非常感谢
发布于 2013-09-03 13:40:39
为什么不能简单地检查一下分机呢?如下所示:
for ArchivesFiles in chemin_zipfiles :
truncated_file, ext = os.path.splitext(os.path.basename(ArchivesFiles))
if not os.path.exists(truncated_file):
os.makedirs(truncated_file)
if ext == 'rar':
arch_ref = rarfile.RarFile(ArchivesFiles,'r')
else:
arch_ref = zipfile.ZipFile(ArchivesFiles,'r')
new_folder = os.path.realpath(truncated_file)
arch_ref.extractall(new_folder)请不要更改,这里有一个truncated_file变量。
另一种可能会使以后的事情变得更容易,可能是这样:
funcs = {'.rar':rarfile.RarFile, '.zip':zipfile.ZipFile}
for ArchivesFiles in chemin_zipfiles :
truncated_file, ext = os.path.splitext(os.path.basename(ArchivesFiles))
if not os.path.exists(truncated_file):
os.makedirs(truncated_file)
arch_ref = funcs[ext](ArchivesFiles,'r')
new_folder = os.path.realpath(truncated_file)
arch_ref.extractall(new_folder)https://stackoverflow.com/questions/18593673
复制相似问题