The lambda function is passed as parameter to a function in which the files are filtered based on the extension. In this method, lambda function is used to filter files with specific extensions to be added in the ZIP file. exists ( 'E:/Zipped file.zip' ) : print ( "ZIP file created" ) else : print ( "ZIP file not created" ) Outputįollowing is an output of the above code − ZIP file createdĬreating ZIP file from specific files in a directory join (folder_name, filename ) # Add files to zip file walk ( 'E:/Folder to be zipped' ) : for filename in file_names : # Create filepath of files in directoryįile_path = os. # Create object of ZipFile with ZipFile ( 'E:/Zipped file.zip', 'w' ) as zip_object : # Traverse all files in directory for folder_name, sub_folders, file_names in os. Exampleįollowing is an example to create ZIP file using multiple files − This is achieved by creating object of ZipFile using with keyword and then writing the files using. In this method, ZipFile() creates a ZIP file in which the files which are to be compressed are added. The zipfile library in python allows for creation of compressed ZIP files using different methods. Compressed ZIP files result in faster file sharing over a network as the size of the ZIP file is significantly smaller than original file. After running the join script, they still may need to run something like zip, gzip, or tar to unpack an archive file, unless its shipped as an executable. exists ( 'E:/Zipped file.zip' ) : print (archived ) else : print ( "ZIP file not created" ) Outputįollowing is an output of the above code − E:\Zipped file.zipĬompressed ZIP files reduce the size of the original directory by applying compression algorithm. make_archive ( 'E:/Zipped file', 'zip', 'E:/Folder to be zipped' ) if os.
0 Comments
Leave a Reply. |