A long time ago (27 days I was asked to write a script to retrieve and extract all tar files named in a specific format on the disk, so I started learning and fishing together.
The script to be written this time aims at the following file structure:
The file structure is shown in the figure above
You can see that there are two tgz compression packages in a tar package. I need to complete the work as shown in the following figure:
PressOn is a good thing
In Python, there are two packages that can call system instructions, one is called sys and the other is called os. What's better is that they don't distinguish operating systems at the high-level language level. The same variable curdir can get the available paths for this operating system under linux and win. os.system() or os.popen() can change the parameters in brackets into the instructions used by the system, as long as the instructions given are correct.
I learned some new things in this script writing, and I can record them here for my own reference.
1 #! usr/bin/python 2 import sys 3 import re 4 import os 5 6 errorMsg = "Usage: python unzipAutoTriage.py [File location]. If File location was ommitted, script will work at current location." 7 8 9 if len(sys.argv) == 1: 10 msg = raw_input("No arguments, script will work at current path(y/n):") 11 if msg != "y": 12 sys.exit(errorMsg) 13 path = "." 14 elif len(sys.argv) >= 3: 15 if re.match(r'~?[a-zA-Z/\_.]*',sys.argv[1]) != None: 16 print "Too many arguments, do you want to execute in", sys.argv[1], "(y/n):" 17 msg = raw_input() 18 if msg != "y": 19 sys.exit(errorMsg) 20 else: 21 path = sys.argv[1] 22 print path 23 24 command = "find " + path + " -mount -name 'auto_triage*.tar'" 25 filelist = os.popen(command).readlines() 26 status = dict.fromkeys(filelist,"Not complete") # Create a dictionary to hold the processing status of each file 27 print "Found" , len(filelist) , "File" 28 if len(filelist) <= 0: 29 sys.exit("No such file, exiting....") 30 31 rootPath = os.path.abspath(os.curdir) # Save the start directory, and then you need to come back to continue 32 33 for path in filelist: 34 command = re.sub(r'auto.*.tar$','',path).replace("\n",'') 35 if os.path.abspath(os.curdir).split("/")[-1] != command.split("/")[-1]: 36 os.chdir(command) # Change directory not available os.system("cd xxxx")This method can't pass 37 print "unzipping", path 38 command = "tar -xvf " + re.sub(r'^\..*/auto','auto',path) 39 res = os.system(command) 40 if res == 0: 41 flag = 2 42 for item in ["spa","spb"]: 43 print "extracting ",item 44 command = "tar -xzvf " + path + item + ".service_dc.tgz -C " + path 45 command = re.sub(r'.tar\n','/',command) 46 res = os.system(command) 47 if res == 0: 48 flag -= flag # Python There is no self increasing or self decreasing in logic! 49 else: 50 status[path] = str(flag) + " unzip subprogress failed" 51 if flag == 0: 52 status[path] = "completed" 53 else: 54 status[path] = "Failed" 55 os.chdir(rootPath) 56 print "****************\nStatus report\n****************" 57 for key,value in status.items(): 58 print '{key}Status: {value}'.format(key = key, value = value)
For the specific pits that you stepped on, you will find https://www.cnblogs.com/jackablack/p/10614686.html In the pit collection!