In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to make a file decompression tool based on Python". In daily operation, I believe many people have doubts about how to make a file decompression tool based on Python. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to make a file decompression tool based on Python". Next, please follow the editor to study!
Often because different compression formats are used to extract files, you need to download different decompression tools to deal with different files, so that there are three or four compression tools on the desktop, so you use python to do a gadget that contains files in a variety of common formats.
The common compression formats are the following four formats:
The compressed files in zip format are generally decompressed using 360compress software.
Compressed files in tar.gz format are generally decompressed using the tar command on the linux system.
Compressed files in rar format are generally decompressed using rar compression software.
The compressed files in 7z format are generally decompressed by 7-zip compression software.
Import a non-standard library for decompression in zip format.
Import osimport zipfile as zip
Write a file compression function in zip decompression format.
Def do_zip (source_, target_file):''zip file compression: param source_: original file path: param target_file: target file path: return:' 'zip_file = zip.ZipFile (target_file,' w') pre_len = len (os.path.dirname (source_)) for parent, dirnames Filenames in os.walk (source_): for filename in filenames: print (f'{filename}') path_file = os.path.join (parent, filename) arcname = path_ file [pre _ len:] .strip (os.path.sep) zip_file.write (path_file, arcname) zip_file.close ()
Write the file decompression function in zip format.
Def un_zip (source_file Target_):''zip file extraction: param source_file: original file path: param target_: destination file path: return:' 'zip_file = zip.ZipFile (source_file) if os.path.isdir (target_): pass else: os.mkdir (target_) for names in zip_file.namelist (): zip_file.extract (names) Target_) zip_file.close ()
Import a non-standard library for decompression in 7z format.
Import py7zr
Write a file compression function in 7z decompression format.
Def do_7z (source_, target_file):''7z File Compression: param source_:: param target_file:: return:''with py7zr.SevenZipFile (target_file,' r') as file: file.extractall (path=source_)
Write the file decompression function in 7z decompression format.
Def un_7z (source_file, target_):''7z file decompression: param source_file:: param target_:: return:''with py7zr.SevenZipFile (source_file,' w') as file: file.writeall (target_)
Import a non-standard library for decompression in rar format.
Import rarfile as rar
Write the file decompression function in rar format.
Def un_rar (source_file, target_):''rar file decompression: param source_file: original file: param target_: destination file path: return:' 'obj_ = rar.RarFile (source_file.decode (' utf-8')) obj_.extractall (target_.decode ('utf-8'))
Next, let's get down to business. First, use the print function to print the menu options, which allow the user to choose the menu after starting the software.
Print ('= PYTHON tool: file extraction software =') print ('description: zip, 7z, rar formats are currently supported) print (' 1, file extraction format: zip/rar/7z') print ('2, file operation type (compression / decompression): YUnip N') print ('3, file path selection, you need to enter the corresponding operation file path') print ('= PYTHON tool: file extraction software =')
Use the input function to receive the unzipped file format entered by the user.
Format_ = input ('Please enter the format to extract the file:\ n')
Use the input function to receive the type of file operation (compression / decompression) entered by the user.
Type_ = input ('Please enter the type of file operation:\ n')
Use the input function to receive the file path entered by the user for the required action.
Source_ = input ('Please enter the storage path (file or directory) of the original file:\ n')
Use the input function to receive the target path of the generated new file entered by the user.
Target_ = input ('Please enter the storage path (file or directory) of the target file:\ n')
In order to maintain the flexibility of input, business judgments of different formats and different types of operations are added.
If format_ = 'zip' and type_ = =' YQing: do_zip (source_, target_) elif format_ = = 'zip' and type_ = =' Nice: un_zip (source_, target_) elif format_ = = 'rar' and type_ = =' YQing: un_zip (source_, target_) elif format_ = = 'rar' and type_ = =' Nice: un_zip (source_, target_) elif format_ = = '7z' and type_ = =' YQing: un_zip (source_) Target_) elif format_ = = '7z' and type_ = =' un_zip: un_zip (source_, target_)
At present, the function point is made in three formats, and the current version may be extended and upgraded later if necessary.
At this point, the study of "how to make a file decompression tool based on Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.