import os import shutil import time # 自动解压文件 path = r'D:\workspace' os.chdir(path) # 必须添加词句,否则会报XXX.zip is not a zip file def scan_file(path): files = os.listdir(path) for file in files: if file.endswith('.zip'): return file def unzip_it(file): folder_name = file.split('.')[0] target_path = path + '/' + folder_name try: os.mkdir(target_path) except Exception as e: pass shutil.unpack_archive(file,target_path) def delete(file): os.remove(file) while True: zip_file = scan_file(path) if zip_file: unzip_it(zip_file) delete(zip_file) time.sleep(2) shutil.unpack_archive(file,target_path):将file压缩到target_path