1 # 编辑者:闫龙 2 # 小程序:根据用户输入选择可以完成以下功能: 3 # 创建文件,如果路径不存在,创建文件夹后再创建文件 4 # 能够查看当前路径 5 # 在当前目录及其所有子目录下查找文件名包含指定字符串的文件 6 import os 7 def CreateDirFile(Dirname,Filename): 8 if(os.path.exists(Dirname)==False): 9 os.makedirs(Dirname) 10 open(os.path.join(Dirname,Filename),"w").close() 11 print("%s目录已创建完成,%s文件已创建完成" % (Dirname, Filename)) 12 elif(os.path.exists(os.path.join(Dirname,Filename))==False): 13 open(os.path.join(Dirname, Filename), "w").close() 14 print("%s中的%s文件已创建完成" % (Dirname, Filename)) 15 def ViewPath(): 16 os.path 17 print(os.getcwd()) 18 def Grep(FindStr): 19 for i in os.walk(os.getcwd()): 20 for j in i[-1]: 21 if(str(j).find(FindStr)>-1): 22 print(os.path.join(i[0],j)) 23 24 p = """1.创建文件夹及路径 25 2.查看当前路径 26 3.查询文件名 27 4.程序退出""" 28 while True: 29 print(p) 30 choice = input("请选择:") 31 if(choice == "1"): 32 Dirname = input("Dirname:") 33 Filename = input("Filename:") 34 CreateDirFile(Dirname, Filename) 35 elif(choice == "2"): 36 ViewPath() 37 elif(choice =="3"): 38 Filename = input("FilenameInStr:") 39 Grep(Filename) 40 elif(choice == "4"): 41 exit() 42 else: 43 continue
时间: 2024-12-22 05:50:09