Simple use of regular expressions, common use of OS modules, and regular expression of OS modules
Simple use of regular expressions
1 import re 2 origin = "3-5*6 + (20-3-4 * (30/5-5) * 6) + (5*6-7 * (20-2)-(8*8) + 195 "3 def f (str): 4 ss = eval (str) 5 return ss 6 while True: 7 res = re. split ("\ ([^ ()] +) \)", origin, 1) 8 if len (res) = 3: 9 befor, mid, after = res10 ret = f (mid) 11 print (type (befor), type (mid), type (after) 12 origin = str (befor) + str (ret) + str (after) 13 print (origin, type (origin) 14 else: 15 rr = f (origin) 16 print (int (rr) 17 break
Common OS modules
OS. stat ('path/filename ') to get the file/directory information
OS. path. dirname (path) returns the path directory
OS. path. exists (path) If path exists, True is returned. If path does not exist, False is returned.
OS. path. join (path1, path2,...) returns the results after combining multiple paths. The parameters before the first absolute path are ignored.