"Python compressed folder" Import "ZipFile" module
1 defZip_ya (startdir,file_news):2Startdir =". \\123" #the folder path to compress3File_news = Startdir +'. zip' #the name of the folder after compression4z = zipfile. ZipFile (File_news,'W', ZipFile. zip_deflated)#parameter one: folder name5 forDirpath, Dirnames, filenamesinchOs.walk (startdir):6Fpath = Dirpath.replace (Startdir,"')#This sentence is very important, do not replace the words, start copying from the root directory7Fpath = Fpath andFpath + os.sepor "'#This sentence understanding I also point depressed, to achieve the current folder and all the files contained in the compression8 forFileNameinchFilenames:9Z.write (Os.path.join (dirpath, filename), fpath+filename)Ten Print('Compression succeeded') One z.close () A -if__name__=="__main__" -Startdir =". \\123" #the folder path to compress theFile_news = Startdir +'. zip' #the name of the folder after compression -Zip_ya (Startdir,file_news)
"Python compressed file" import "ZipFile" module
ImportZipFiledefzip_files (Files, zip_name): Zip= ZipFile. ZipFile (Zip_name,'W', ZipFile. zip_deflated) forFileinchFiles:Print('compressing', file) zip.write (file) zip.close ()Print('Compressing finished') files= ['. \\123.txt','. \\3.txt']#file location, multiple files separated by ","Zip_file ='. \\m66y.zip'#Compress Package NameZip_files (Files, zip_file)
Python compresses files/folders with Python