1

我有以下目录,在父目录中,有几个文件夹可以说 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 等以将它们包含在文件将被解压缩的路径中。因此,在父目录中创建了解压缩的文件夹。有任何想法吗?

4

2 回答 2

2

您拥有的代码似乎工作正常,您只是为了确保您没有覆盖变量名称并使用正确的名称。以下代码非常适合我

import os
import zipfile
import glob

for path in glob.glob('./*/xxx*xxxx*'): ##walk the dir tree and find the files of interest

    zf = os.path.basename(path) #save the zipfile path
    zip_ref = zipfile.ZipFile(path, 'r') 
    zip_ref.extractall(path.replace(r'.zip', '')) #unzip to a folder without the .zip extension
于 2017-08-17T13:25:48.260 回答
1

与其尝试在单个语句中执行此操作,不如先获取所有文件夹的列表,然后获取每个文件夹内的文件列表,这样做会更容易、更易读。例子 -

import os.path
for folder in glob.glob("./*"):
    #Using *.zip to only get zip files
    for path in glob.glob(os.path.join(".",folder,"*.zip")):
        filename = os.path.split(path)[1]
        if folder in filename:
            #Do your logic
于 2017-08-17T10:56:41.023 回答