#python3 backupToZip() #copies an entire folder and its contents into a zip file whose filename increments import zipfile, os,datetime path = 'E:\\04.AutomationProject\\PracticePython\\noteBasic' os.chdir(path) def backupToZip(folder): #backup the entire contents of foler into zip file folder = os.path.abspath(folder) #figure out the filename this code should be based on #whatif fiels already exitst nowTime = datetime.datetime.now().strftime("%Y%m%d%H%M") while True: zipFilename = os.path.basename(folder) + "_" + str(nowTime) + ".zip" if not os.path.exists(zipFilename): break nowTime = datetime.datetime.now().strftime("%Y%m%d%H%M") #todo:create the zip file print("creating %s..." %(zipFilename)) backupZip = zipfile.ZipFile(zipFilename,'w') #todo:walk the entire folder tree and compress the files in each folder for folderName,subFolders,fileNames in os.walk(folder): print('adding files in %s...'%(folderName)) #add the current folder to the ZIP file for fileName in fileNames: newBase = os.path.basename(folder) + '_' if fileName.startswith(newBase) and fileName.endswith(".zip"): continue #don't backup the backupzip files backupZip.write(os.path.join(folderName,fileName)) backupZip.close() print('Done') backupToZip(path)
003.备份文件夹zip
猜你喜欢
转载自blog.csdn.net/baidu_27361307/article/details/80973008
今日推荐
周排行