解压缩文件夹和子文件夹中的 zip 文件 [英] Unzip zip files in folders and subfolders

查看:73
本文介绍了解压缩文件夹和子文件夹中的 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试解压缩 150 个 zip 文件.所有 zip 文件名称不同,它们都分布在一个大文件夹中,该文件夹分为许多子文件夹和子子文件夹.我想将每个存档解压缩到与原始 zip 文件名相同名称的单独文件夹中,并且在与原始 zip 文件相同的位置.我的代码是:

I try to unzip 150 zip files. All the zip files as different names, and they all spread in one big folder that divided to a lot of sub folders and sub sub folders.i want to extract each archive to separate folder with the same name as the original zip file name and also in the same place as the original zip file . my code is:

import zipfile    
import os,os.path,sys  

pattern = '*.zip'  
folder = r"C:\Project\layers"   
files_process = []  
for root,dirs,files in os.walk(r"C:\Project\layers"):  
    for filenames in files:  
        if filenames == pattern:  
            files_process.append(os.path.join(root, filenames))  
            zip.extract() 

在我运行代码后什么也没发生.提前感谢您对此的任何帮助.

After i run the code nothing happened. Thanks in advance for any help on this.

推荐答案

更新:

最后,这段代码对我有用:

Finally, this code worked for me:

import zipfile,fnmatch,os

rootPath = r"C:\Project"
pattern = '*.zip'
for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        print(os.path.join(root, filename))
        zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))

这篇关于解压缩文件夹和子文件夹中的 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆