1. Pre-preparation
python decompress rar format files, you need to install the rarfile module, go to the Internet to download rarfile, I downloaded here is rarfile-2., decompression also need to correspond to the
2. Tool installation
Copy rarfile-2. to the Scripts directory of the python installation path:
cmd into python's Scripts directory and run pip install rarfile-2. The installation was successful as shown below:
3. Unzip the rar
import os import rarfile base_path = r'F:\python\rar' def unrar(base_path): files = (base_path) () for path in files: full_path = (base_path, path) print(full_path) z = (full_path) (base_path) () (full_path)
4, compressed rar file
def compress(input_file, output_file, root_path, rar_path='D:/"Program Files"/WinRAR/'): """ Call CMD command to compress files/folders Parameters ---------- input_file : Name of the file/folder to be compressed. Compression will start from whichever level of the directory it starts from ; output_file : the output path of the compressed file and the name of the compressed file. Can be .rar, .zip; root_path : the directory where input_file is located. root_path: input_file directory; rar_path : WinRAR software installation path, the default is 'C:/"'. The default is 'C:/"Program Files'/WinRAR/'. NOTE: Be sure to add extra heavy quotes when there are spaces in paths and file names! """ cmd_command = r'%s a %s %s' % (rar_path, output_file, input_file) print(root_path) (root_path) # Switching working directories print(cmd_command) (cmd_command) if (cmd_command)==0: print('Successful backup to', output_file) else: print('Backup FAILED', input_file) def rar(paths): files = (paths) for path in files: input_file = '"' + path + '"' out = ('.')[0] + '_bak.rar' out_file = '"' + out + '"' print(path) print(out) compress(input_file,out_file,paths)
def main(): unrar(base_path) rar(base_path) if __name__ == "__main__": main()
[Attention]:
You need to put the files in the same directory as the python script, otherwise the extracted files will not be generated, even though the script runs successfully.
P.S. Python batch compression and decompression of files (zip, rar)
# -*- coding: utf-8 -*- """ RT @Time : 2023/8/28 14:47 RT @Auth : RS Lost Little Bookie @File : Compress and Decompress @IDE : PyCharm @Purpose : Batch Compress/Decompress Folders """ import os import zipfile def Compress_path_zip(path_all): path_all_list = (path_all) # List all folders in the general folder that need to be compressed for path_each in path_all_list: path_compress = (path_all, path_each) # Absolute path to be compressed if (path_compress): print("Compressing: %s" % path_each) # Iterate over all folders to be compressed if (path_all + "%" % path_each): print(path_all + "%" % path_each+"Already exists, new file name '%s'" % (path_all + "%" % path_each)) zip_file = (path_all + "%" % path_each, 'w', zipfile.ZIP_DEFLATED, allowZip64=True) # Compression objects created to perform subsequent operations # file_list = zip_file.namelist() # List all files in the compressed folder else: zip_file = (path_all + "%" % path_each, 'w', zipfile.ZIP_DEFLATED, allowZip64=True) file_list = (path_compress) # All files in the folder to be compressed # zip_file.setpassword(b'123') for file_each in file_list: # Iterate over all files in the folder file_path = (path_compress, file_each) zip_file.write(file_path, file_each) zip_file.close() else: print(path_each, "Not a folder, no compression.") continue def Decompress_path_zip(path_all): path_all_list = (path_all) for path_each in path_all_list: # Iterate over all folders to be compressed if path_each.endswith('.zip'): print("Decompressing: %s" % path_each) path_decompress = (path_all, path_each) zip_file = (path_decompress, 'r') # Location of compressed file for file in zip_file.namelist(): if (path_decompress[:-4]): print("'%s'" % path_decompress[:-4], "Already exists, new file name '%s'" % (path_decompress[:-4] + "1")) zip_file.extract(file, path_decompress[:-4] + "1") # Unzip location,pwd="1234".encode('utf-8') else: zip_file.extract(file, path_decompress[:-4]) # Unzip location zip_file.close() else: print(path_each, "Non-zip file, no decompression!") continue if __name__ == "__main__": path = "G:/try/" # Compress_path_zip(path) Decompress_path_zip(path)
summarize
to this article on python rar decompression and compression of the article is introduced to this, more related python rar decompression and compression content, please search my previous articles or continue to browse the following related articles I hope you will support me in the future!