Python extract the rar, zip, tar files

Q1: How to extract the rar archive file?

A :

  • - Step1: Check for rarfile third-party libraries, if not this module, you need to install;
  • - Step2: Reference code is as follows:

 

Import rarfile
 Import OS 

RAR = rarfile.RarFile (filename, mode = ' R & lt ' ) # value of mode can only 'R & lt' 

# determines whether there is a folder with the same name, if there is created with the same name folder 
IF os.path.isdir (os.path.splitext (filename) [0]): 
rf_list = rf.namelist () # obtain all the files compressed package 
Print ( ' RAR file content ' , rf_list) 
 the else : 
os.mkdir (os.path.splitext (filename) [0]) 

rar.extractall (os.path.splitext (filename) [0]) # -extracting file 
rar.close () # close the file, you must have free memory

 

 

 

Q2: How to extract the zip archive file?
A:
can RPA Designer comes] [zip decompression assembly, writing code, you can refer to the following (note zipfile check whether third-party libraries, designer comes, can be called directly):

Import ZipFile
 Import OS 

zip_file = zipfile.ZipFile (filename)
 IF os.path.isdir (os.path.splitext (filename) [0]): 
zip_list = zip_file.namelist () # obtain all the files compressed package 
Print ( ' ZIP file content ' , zip_list) 
 the else : 
os.mkdir (os.path.splitext (filename) [0]) 

for F in zip_list: 
zip_file.extract (F, os.path.splitext (filename) [0]) # cycles decompression file to a specified directory 

zip_file.close ()

 

Q3: How to extract the tar package file?

A:
Note whether the installation tarfile third-party libraries, RPA designer comes, can be called directly:

Import tarfile
 Import OS 

the tar = tarfile.open (filename, MODE = " R & lt: GZ " ) # "R & lt: GZ" indicates for Reading with the gzip compression Open 
tar.extractall (os.path.splitext path = (filename) [0] ) # Extract the tar.gz file to the temp folder 
tar.close ()

 Free Trial: https://support.i-search.com.cn/

Guess you like

Origin www.cnblogs.com/isearch/p/11888839.html