import os,zipfile #创建函数主体 def backupToZip(path): path = os.path.abspath(path) number = 1 while True: Zipname = os.path.basename(path)+'_'+str(number)+'.zip' if not os.path.exists(Zipname): break number += 1 print('创建%s的压缩备份包' % Zipname) Zipfile = zipfile.ZipFile(Zipname, 'w') for foldername, subfolders, filenames in os.walk(path): print('开始压缩%s...' % foldername) Zipfile.write(foldername) for filename in filenames: newBase = os.path.basename(path) + '_' if filename.startswith(newBase) and filename.endswith('.zip'): continue Zipfile.write((os.path.join(foldername, filename))) Zipfile.close() print('完成!') backupToZip('E:\\nametest')