Dzwebs.Net

撰写电脑技术杂文十余年

python自动解压指定目录及子目录下的所有ZIP文件到相同路径

Admin | 2022-12-8 9:30:28 | 被阅次数 | 612

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  代码功能:自动解压指定目录及子目录下的所有ZIP文件到相同路径

import logging

from pathlib import Path
from shutil import unpack_archive
import os, shutil
import uuid


#对指定目录,扫描目录及子目录中所有的ZIP文件并解压到相同目录
def MyUnZip(path):
    zip_files = Path(path).rglob("*.zip")
    while True:
        try:
            path = next(zip_files)
            #print(path)
        except StopIteration:
            break # no more files
        except PermissionError:
            logging.exception("permission error")
        else:
            try:
                extract_dir = path.with_name(path.stem)
                #print(extract_dir)
                unpack_archive(str(path), str(extract_dir),'zip')
            except :
                print('这个出错了:',extract_dir)
                uuid_str = uuid.uuid4().hex
                tmp_file_name = 'tmpfile_%s.zip' % uuid_str
                try:
                    shutil.copyfile(str(path), r'C:\MyZip\why\{}'.format(tmp_file_name))
                except :
                    pass

该杂文来自: 最新技术

上一篇:python对字典进行排序

下一篇:python之pandas读取对某工作表或所有工作表的代码

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计