os--模块
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))
?
轉載于:https://www.cnblogs.com/baiby/p/10833741.html
總結
- 上一篇: 新五丰股票历史最低价位多少 投资时选择
- 下一篇: 达飞云贷质保金怎么退