. 1 # ! / Usr / bin / to python3 2 # - * - Coding: UTF-. 8 - * - . 3 Import OS . 4 Import the shutil . 5 Import Time . 6 Import SYS . 7 Import The subprocess . 8 sys.setrecursionlimit (10000) # Set the maximum recursive function depth, prevent infinite recursion causes a stack overflow and crash 9 10 class UnzipLogFile: 11 parentName = " D: \ ERD Guangdong inspection log " 12 DEF __init__ (Self, filePath): 13 self.parentName = filePath 14 DEF Iszip (Self, File): # Here custom function determines the file type is not compressed packet type 15 the compress = [ " .tar.gz " , " .tar.bz2 " , " .tar.bz " , " .tar .tgz " , " .tar " , " .tgz " , " .zip " , " .rar " , " .7z " ] 16 for Z in the compress: . 17 IF File.endswith(z):#Description: determining whether the string end to the specified character or substring. 18 is return Z . 19 20 is # determines whether BDC log file 21 is DEF IsMrsLogFile (Self, fileName): 22 is [dirname, filename] = The os.path.split (fileName ) 23 mrsFiles the SET = (( " ubp_bdc01_20 " , " ubp.info.20 " )) 24- for namefile in mrsFiles: 25 IF namefile in filename: 26 return True 27 the else : 28 the Continue 29 return False 30 31 is # compressed file decompression operation 32 DEF the Unzip (Self, srcFile, FOLDER_NAME, dstDir): 33 is the os.chdir (FOLDER_NAME) 34 is IF self.IsMrsLogFile (srcFile) == False: 35 return 36 Order = " \ "C: \\ Program Files \\ \\ 7z.exe. 7-the Zip \" X \ "{0} \" -R & lt -o " + dstDir 37 [ cmd = order.format (srcFile) 38 is Print ( " extracting: " , cmd) 39 os.popen (cmd) # call the command line console program 40 the time.sleep (. 5 ) 41 is # shutil.move (STR (Order), dstDir) 42 is 43 is # obtain all the files in a path 44 is DEF AllFile (Self, filePath): 45 the os.chdir (filePath) # the os.chdir path () method for changing the current working directory to the specified 46 is path the os.getcwd = () # the os.getcwd () method returns the current working directory 47 file_names the os.listdir = ( " ./ " ) # the os.listdir list the name of the file or files () method returns the specified folder contains the folder. This list is in alphabetical order. It does not include the '.' And '..' even if it is in the same folder. It supports only be used under Unix, Windows. 48 for name in file_names: 49 path = + name_path " \\ " + name # where necessary 50 IF The os.path.isfile (name_path): # determine whether a file path 51 is IF self.Iszip (name) in ( " .tar.gz " , " .tar.bz2 " , " .tar.bz " , " .tar.tgz " , " .tar " , " .tgz " , " .zip " , " .rar " ,".7z " :) 52 is filedir = os.path.splitext (name) [0] # tuples split path, return path name and file extension 53 is IF os.path.exists (filedir): # If path path exists, returns true;. path if the path does not exist, returns False 54 is shutil.rmtree (filedir) # recursively delete a directory and all the contents in the directory 55 srcFile = STR (path) + " \\ " + STR (name) 56 is dstDir STR = (path) + " \\ " + STR (filedir) 57 is IF self.IsMrsLogFile (srcFile) == False: 58 Continue 59 Print ( " Create Directory: " + filedir) 60 os.mkdir (filedir) 61 is self.Unzip (srcFile, path, dstDir) 62 is self.AllFile (dstDir) 63 is elif os.path.isdir (name_path): # Analyzing whether the path for the folder 64- self.AllFile (path + " \\ " + name) 65 os.chdir (filePath) 66 path = os.getcwd () 67 68 DEF the Run (Self): 69 self.AllFile ( " D: \ ERD Guangdong inspection log " ) 70 71 is 72 '' ' 73 is RUN = UnzipLogFile ( "D: \ inspection log storage path") 74 run.Run (); 75 ' ''