1 #edited by: Shanlong2 #applet: According to user input selection can complete the following functions:3 #Create a file, create a folder, and then create a file if the path does not exist4 #ability to view current path5 #find file names in the current directory and all subdirectories that contain the specified string6 ImportOS7 defCreatedirfile (dirname,filename):8 if(os.path.exists (Dirname) = =False):9 os.makedirs (Dirname)TenOpen (Os.path.join (dirname,filename),"W"). Close () One Print("%s directory created complete,%s file created"%(Dirname, Filename)) A elif(Os.path.exists (Os.path.join (dirname,filename)) = =False): -Open (Os.path.join (Dirname, Filename),"W"). Close () - Print("%s file in%s was created"%(Dirname, Filename)) the defViewPath (): - Os.path - Print(OS.GETCWD ()) - defGrep (FINDSTR): + forIinchOs.walk (OS.GETCWD ()): - forJinchI[-1]: + if(Str (j). Find (FINDSTR) >-1): A Print(Os.path.join (i[0],j)) at -p ="""1. Creating folders and Paths - 2. View the current path - 3. query file name - 4. Program Exit""" - whileTrue: in Print(P) -Choice = input ("Please select:") to if(Choice = ="1"): +Dirname = input ("Dirname:") -Filename = input ("Filename:") the createdirfile (Dirname, Filename) * elif(Choice = ="2"): $ ViewPath ()Panax Notoginseng elif(Choice = ="3"): -Filename = input ("Filenameinstr:") the Grep (Filename) + elif(Choice = ="4"): A exit () the Else: + Continue
Python Practice-os Module Exercise-that's the thing.