【发布时间】:2018-01-25 17:47:23
【问题描述】:
我有以下目录,在父目录中有几个文件夹,比如说 ABCD,每个文件夹中有许多 zip,名称中显示的名称和父文件夹的字母以及其他信息:
-parent--A-xxxAxxxx_timestamp.zip
-xxxAxxxx_timestamp.zip
-xxxAxxxx_timestamp.zip
--B-xxxBxxxx_timestamp.zip
-xxxBxxxx_timestamp.zip
-xxxBxxxx_timestamp.zip
--C-xxxCxxxx_timestamp.zip
-xxxCxxxx_timestamp.zip
-xxxCxxxx_timestamp.zip
--D-xxxDxxxx_timestamp.zip
-xxxDxxxx_timestamp.zip
-xxxDxxxx_timestamp.zip
我只需要解压缩此树中选定的 zip,并将它们放在同名且不带 .zip 扩展名的同一目录中。
输出:
-parent--A-xxxAxxxx_timestamp
-xxxAxxxx_timestamp
-xxxAxxxx_timestamp
--B-xxxBxxxx_timestamp
-xxxBxxxx_timestamp
-xxxBxxxx_timestamp
--C-xxxCxxxx_timestamp
-xxxCxxxx_timestamp
-xxxCxxxx_timestamp
--D-xxxDxxxx_timestamp
-xxxDxxxx_timestamp
-xxxDxxxx_timestamp
我的努力:
for path in glob.glob('./*/xxx*xxxx*'): ##walk the dir tree and find the files of interest
zipfile=os.path.basename(path) #save the zipfile path
zip_ref=zipfile.ZipFile(path, 'r')
zip_ref=extractall(zipfile.replace(r'.zip', '')) #unzip to a folder without the .zip extension
问题是我不知道如何保存 A、B、C、D 等以将它们包含在文件将被解压缩的路径中。因此,在父目录中创建了解压缩的文件夹。有什么想法吗?
【问题讨论】:
-
与其一次性尝试,不如先获取
.中所有文件夹的列表,然后获取每个文件夹中所有文件的列表,并检查其中是否出现文件夹名称。
标签: python unzip directory-tree