In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the compression and decompression methods of Python files?" in the operation of actual cases, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Foreword:
Python has been widely used in artificial intelligence, background service and other fields. Because python has a large number of third-party open source packages and its easy-to-use language features, this language has strong advantages in data processing, batch processing and other methods, and is quite convenient to use.
Installation:
Pip install zipfile
Code:
1. Compress the entire folder import zipfileimport osfrom tqdm import tqdmdef zip_file (src_dir) Save_name='default'):''all files and folders under the compressed folder default compressed file name: folder name default compressed file path: folder upper directory''if save_name= = 'default': zip_name = src_dir +' .zip 'else: if save_name is None or save_name= ='': Zip_name = src_dir + '.zip' else: zip_name = save_name + '.zip' z = zipfile.ZipFile (zip_name For dirpath, dirnames, filenames in os.walk (src_dir): fpath = dirpath.replace (src_dir,'') fpath = fpath and fpath + os.sep or''for filename in filenames: z.write (os.path.join (dirpath, filename), fpath + filename) z.close () return True2, compression specified extension file def zip_file_by_extension (src_dir) Extension='dll', save_name='default'):''filter the files to be compressed according to the file extension Note: for single-tier directories The subfiles under the folder will not be compressed''if save_name = = 'default': zip_name = src_dir +' .zip 'else: if save_name is None or save_name = =': zip_name = src_dir + '.zip' else: zip_name = save_name + '.zip' z = zipfile.ZipFile (zip_name,'w') Zipfile.ZIP_DEFLATED) file_list = os.listdir (src_dir) for filename in tqdm (file_list): if not os.path.isfile (os.path.join (src_dir, filename)): continue if filename.split ('.) [- 1] = = extension: print (r 'compressing files:', filename, end='') z.write (os.path.join (src_dir, filename) Filename) z.close () return True3, get the list of files to be compressed of the specified type def get_zip_file_list (src_dir, extension='dll'):''get the list of files to be compressed' 'file_list = os.listdir (src_dir) zip_list = [] for filename in file_list: if not os.path.isfile (os.path.join (src_dir) Filename): continue if filename.split ('.) [- 1] = = extension: zip_list.append (filename) print (zip_list) 4, decompress the file def unzip_file (zip_src, dst_dir):''decompress''r = zipfile.is_zipfile (zip_src) if r: fz = zipfile.ZipFile (zip_src) 'r') for file in fz.namelist (): fz.extract (file, dst_dir) else: print ('This is not zip') return False return True "what is the method of compressing and decompressing Python files?" that's all. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.