Created
April 4, 2018 11:51
-
-
Save secsilm/588805334dcb0f5923d473042c21594d to your computer and use it in GitHub Desktop.
将存在于多个文件夹中的 zip 文件解压到另一个目录下的独立文件夹
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import shutil | |
import warnings | |
import zipfile | |
from pathlib import Path | |
# zip 文件所在的地址 | |
in_path = Path('D:\BaiduYunDownload\Jay Chou') | |
# 解压地址 | |
out_path = Path('D:\BaiduYunDownload') | |
def extract(filepath, output_path): | |
"""解压 filepath 文件到 output_path。 | |
Args: | |
filepath (Path 类型): zip 文件的路径,必须是 Path 类型 | |
output_path (Path 类型): 输出文件的目录 | |
""" | |
f = zipfile.ZipFile(filepath, 'r') | |
for fileinfo in f.infolist(): | |
filename = fileinfo.filename.encode('cp437').decode('gbk') | |
outputfile = open(output_path.joinpath(filename), "wb") | |
shutil.copyfileobj(f.open(fileinfo.filename), outputfile) | |
outputfile.close() | |
f.close() | |
for file in in_path.rglob('*.zip'): | |
print(f'Extracting {file}') | |
filename = re.search('《(.*)》', file.stem).group(1) | |
filepath = out_path.joinpath(filename) | |
try: | |
filepath.mkdir() | |
except FileExistsError: | |
warnings.warn(f'{filepath} 已存在,文件将会解压到其中。') | |
extract(file, filepath) | |
print('Done!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment