Article From:

1)How to compress and backup multiple files

Using zipfileCreate a compressed filesee informationdecompressionCreationImport zipfileOs.chdir ('test')My_zip = zipfile.ZipFiLe ('','w') create objects, write filesMy_zip.write?My_zip.write ('1.pdf', compress_type=zipfile.Z)IP_DEFLATED)My_zip.write ('1.doc', compress_type=zipfile.ZIP_DEFLATED)My_zip.close () closed file closes the fileLook atXXX = zipfile.ZipFile ('')Xxx.namelist ()DecompressXXX = zipfile.ZipFile ('')Xxx.extractall (')' parameter is optional, current directory, or specified.Xxx.close ()

2) Code backup machine

Packing compressionAutomatic namingFile screening before packing and only backup specific files(timingPackingImport zipfileImport OSDef zip_all (from_dir, target_name):""Write each file in the directory to the zip file" ""My_zip = zipfile.ZipFile (target_name,'w') to create objects and write files.My_zip.writeFor root, dirs, files in os.walk (from_dir):For name in files:Filename = os.The name of the file in the path.join (root, name) root directoryPrint (filename)My_zip.write (filename, COMP)Ress_type=zipfile.ZIP_DEFLATED)My_zip.close ()) close the file and note that when all the final files are finished, then close.Automatic naming, with an increase as an exampleDeF auto_name (source_name):New_name =''To judge whether a document exists or not, if simply determines whether it is empty or not.It also needs to be further judgedIf sOurce_name:,, A-11.zip1.split resolutionNew_name = source_name.Split ('-') [0] + '-'+ str (int (source_name.split ('-') [1].split (".") [0]) +1).+'.ziP'2, regularReturn new_nameFile screeningDef zip_all_by_name (from_dir, target_name, keywords):PassTestBase_dir = r'C:\Users\sothi\Desktop\py2018\02-auto\test'Target = os.path.join (base_dir, auto_name (''))
Link of this Article: Python base — compressed file

Leave a Reply

Your email address will not be published. Required fields are marked *