fileSet = set() for file in files: if os.path.isdir(file):#如果是文件夹则 file = file[:-4] fileSet.add(file) defget_zip_file(input_path, result): """ 对目录进行深度优先遍历 :param input_path: :param result: :return: """ files = os.listdir(input_path) for file in files: if os.path.isdir(input_path + '/' + file): get_zip_file(input_path + '/' + file, result) else: result.append(input_path + '/' + file)
for fs in fileSet: fzip = fs + ".zip" with ZipFile(fzip, 'w') as myzip: #filelists = [] for file in files: if os.path.isdir(file):#如果是文件夹则 if fs in file: filelists = [] get_zip_file(file, filelists) for f in filelists: myzip.write(f)