1 import os 2 3 print(os.getcwd()) #查询当前目录 4 运行结果:E:\LionUIAuto\day6 5 6 os.mkdir(r\'E:\LionUIAuto\day5\test\') #通过绝对路径来在某个路径下创建目录 7 8 os.chdir(r\'C:\Users\dell-3020\Desktop\') #改变目录 9 print(os.getcwd()) 10 open(\'bai.hhh\',\'w\') #并在改变了的目录下创建了一个文件 11 ====================================================================== 12 13 ps:os.system 能帮你执行命令,但是帮 你拿不到命令的结果 14 os.system(\'ipconfig\') #用来执行操作系统命令 15 os.system(\'dir\') #查看当前目录下有哪些东西 16 17 res = os.system(\'ipconfig\') #用来执行操作系统命令 18 print(\'-------\',res) 19 运行结果:------- 0 备注:返回0代表命令执行成功 20 ================================================================= 21 22 res = os.popen(\'ipconfig\').read() #可以用来拿到结果 23 #先执行用popen执行命令然后用这个命令的read方法读出来返回结果 24 print(res) 25 运行结果:Windows IP 配置 26 27 以太网适配器 本地连接: 28 29 连接特定的 DNS 后缀 . . . . . . . : 30 本地链接 IPv6 地址. . . . . . . . : xxxxxxxxxxxxxxxxxxxxxx 31 IPv4 地址 . . . . . . . . . . . . : IP地址 32 子网掩码 . . . . . . . . . . . . : IP地址 33 默认网关. . . . . . . . . . . . . : IP地址 34 35 隧道适配器 isatap.{xxxxxxxxxxxxxxxxxxxxxx}: 36 37 媒体状态 . . . . . . . . . . . . : 媒体已断开 38 连接特定的 DNS 后缀 . . . . . . . : 39 ======================================================================= 40 41 print(os.cpu_count()) #获取cpu的个数 42 43 os.path.getctime() #文件创建时间 44 os.path.getatime() #文件最后访问时间 45 os.path.getmtime() #文件最后修改时间 46 47 res = os.path.join(\'LionUIAuto\',\'day6\',\'hawu.txt\') 48 print(res) 49 #比如window和mac的展示路径的方式就不一样,用join拼路径的时候,就不用管路径分割符了,直接写路径就行 50 运行结果:LionUIAuto\day6\hawu.txt 51 52 p = \'E:\LionUIAuto\day6\' 53 print(os.path.exists(p)) #判断目录是否存在的 54 运行结果:True 55 56 p = r\'E:\LionUIAuto\day6\os模块.py\' 57 res = os.path.split(p) #分割文件路径和文件名 58 print(res) 59 运行结果:(\'E:\\LionUIAuto\\day6\', \'os模块.py\') 60 61 p = r\'E:\LionUIAuto\day6\os模块.py\' 62 res = os.path.dirname(p) #取父目录的 63 print(res) 64 运行结果:E:\LionUIAuto\day6 65 66 res = os.path.abspath(\'../day6\') #根据相对路径获取绝对路径的 67 print(res) 68 运行结果:E:\LionUIAuto\day6 69 70 -================================================================== 71 # os.walk() 72 73 count = 0 74 for a,b,c in os.walk(r\'E:\LionUIAuto\day6\abc\'): 75 print(\'a\',a) #cur_dir 当前循环的目录 76 print(\'b\',b) #dirs 当前目录下所有的文件夹 77 print(\'c\',c) #files 当前目录下所有的文件 78 count+=1 79 print(\'============\') 80 if count>1: 81 break 82 运行结果: 83 a E:\LionUIAuto\day6\abc 84 b [\'bcd1\', \'dcd\'] 85 c [] 86 ============ 87 a E:\LionUIAuto\day6\abc\bcd1 88 b [\'ddd\'] 89 c [\'a.txt\'] 90 ============ 91 92 #找某个目录下所有以.txt结尾的文件 93 count = 0 94 for cur_dir,dirs,files in os.walk(r\'E:\LionUIAuto\day6\abc\'): #for循环的时候一定要写3个参数 95 print(\'cur_dir\',cur_dir) 96 print(\'dirs\',dirs) 97 for f in files: 98 if f.endswith(\'.txt\'): 99 count+=1 100 print(\'count\',count) 101 102 运行结果: 103 cur_dir E:\LionUIAuto\day6\abc 104 dirs [\'bcd1\', \'dcd\'] 105 cur_dir E:\LionUIAuto\day6\abc\bcd1 106 dirs [\'ddd\'] 107 cur_dir E:\LionUIAuto\day6\abc\bcd1\ddd 108 dirs [] 109 cur_dir E:\LionUIAuto\day6\abc\dcd 110 dirs [] 111 count 2 112 113 ====================================================================== 114 #找到某位同学电脑里面存的,mp4的电影有多少部,找出来在哪个目录下存着 115 116 for cur_dir,dirs,files in os.walk(r\'E:/\'): 117 print(\'当前正在%s目录下查找\'%cur_dir) 118 for f in files: 119 if f.endswith(\'.mp4\'): 120 real_path = os.path.join(cur_dir,f) #找到文件的真实路径去删除文件 121 os.remove(real_path) #删除文件 122 # print(\'发现一个小电影[%s],目录正在[%s]\' %(f,cur_dir)) 123 ======================================================================== 124 125 for cur_dir,dirs,files in os.walk(r\'E:/\'): 126 print(\'当前正在%s目录下查找\'%cur_dir) 127 for f in files: 128 if \'basttest\' in f: #查询某个关键字是否在文件中 129 real_path = os.path.join(cur_dir,f) #找到文件的真实路径去删除文件 130 os.remove(real_path) #删除文件 131 # print(\'发现一个小电影[%s],目录正在[%s]\' %(f,cur_dir))