日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

os--模块

發(fā)布時(shí)間:2024/10/12 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 os--模块 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 import os 2 3 print(os.getcwd()) #查詢當(dāng)前目錄 4 運(yùn)行結(jié)果:E:\LionUIAuto\day6 5 6 os.mkdir(r'E:\LionUIAuto\day5\test') #通過(guò)絕對(duì)路徑來(lái)在某個(gè)路徑下創(chuàng)建目錄 7 8 os.chdir(r'C:\Users\dell-3020\Desktop') #改變目錄 9 print(os.getcwd()) 10 open('bai.hhh','w') #并在改變了的目錄下創(chuàng)建了一個(gè)文件 11 ====================================================================== 12 13 ps:os.system 能幫你執(zhí)行命令,但是幫 你拿不到命令的結(jié)果 14 os.system('ipconfig') #用來(lái)執(zhí)行操作系統(tǒng)命令 15 os.system('dir') #查看當(dāng)前目錄下有哪些東西 16 17 res = os.system('ipconfig') #用來(lái)執(zhí)行操作系統(tǒng)命令 18 print('-------',res) 19 運(yùn)行結(jié)果:------- 0 備注:返回0代表命令執(zhí)行成功 20 ================================================================= 21 22 res = os.popen('ipconfig').read() #可以用來(lái)拿到結(jié)果 23 #先執(zhí)行用popen執(zhí)行命令然后用這個(gè)命令的read方法讀出來(lái)返回結(jié)果 24 print(res) 25 運(yùn)行結(jié)果:Windows IP 配置 26 27 以太網(wǎng)適配器 本地連接: 28 29 連接特定的 DNS 后綴 . . . . . . . : 30 本地鏈接 IPv6 地址. . . . . . . . : xxxxxxxxxxxxxxxxxxxxxx 31 IPv4 地址 . . . . . . . . . . . . : IP地址 32 子網(wǎng)掩碼 . . . . . . . . . . . . : IP地址 33 默認(rèn)網(wǎng)關(guān). . . . . . . . . . . . . : IP地址 34 35 隧道適配器 isatap.{xxxxxxxxxxxxxxxxxxxxxx}: 36 37 媒體狀態(tài) . . . . . . . . . . . . : 媒體已斷開 38 連接特定的 DNS 后綴 . . . . . . . : 39 ======================================================================= 40 41 print(os.cpu_count()) #獲取cpu的個(gè)數(shù) 42 43 os.path.getctime() #文件創(chuàng)建時(shí)間 44 os.path.getatime() #文件最后訪問(wèn)時(shí)間 45 os.path.getmtime() #文件最后修改時(shí)間 46 47 res = os.path.join('LionUIAuto','day6','hawu.txt') 48 print(res) 49 #比如window和mac的展示路徑的方式就不一樣,用join拼路徑的時(shí)候,就不用管路徑分割符了,直接寫路徑就行 50 運(yùn)行結(jié)果:LionUIAuto\day6\hawu.txt 51 52 p = 'E:\LionUIAuto\day6' 53 print(os.path.exists(p)) #判斷目錄是否存在的 54 運(yùn)行結(jié)果:True 55 56 p = r'E:\LionUIAuto\day6\os模塊.py' 57 res = os.path.split(p) #分割文件路徑和文件名 58 print(res) 59 運(yùn)行結(jié)果:('E:\\LionUIAuto\\day6', 'os模塊.py') 60 61 p = r'E:\LionUIAuto\day6\os模塊.py' 62 res = os.path.dirname(p) #取父目錄的 63 print(res) 64 運(yùn)行結(jié)果:E:\LionUIAuto\day6 65 66 res = os.path.abspath('../day6') #根據(jù)相對(duì)路徑獲取絕對(duì)路徑的 67 print(res) 68 運(yùn)行結(jié)果: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 當(dāng)前循環(huán)的目錄 76 print('b',b) #dirs 當(dāng)前目錄下所有的文件夾 77 print('c',c) #files 當(dāng)前目錄下所有的文件 78 count+=1 79 print('============') 80 if count>1: 81 break 82 運(yùn)行結(jié)果: 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 #找某個(gè)目錄下所有以.txt結(jié)尾的文件 93 count = 0 94 for cur_dir,dirs,files in os.walk(r'E:\LionUIAuto\day6\abc'): #for循環(huán)的時(shí)候一定要寫3個(gè)參數(shù) 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 運(yùn)行結(jié)果: 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 #找到某位同學(xué)電腦里面存的,mp4的電影有多少部,找出來(lái)在哪個(gè)目錄下存著 115 116 for cur_dir,dirs,files in os.walk(r'E:/'): 117 print('當(dāng)前正在%s目錄下查找'%cur_dir) 118 for f in files: 119 if f.endswith('.mp4'): 120 real_path = os.path.join(cur_dir,f) #找到文件的真實(shí)路徑去刪除文件 121 os.remove(real_path) #刪除文件 122 # print('發(fā)現(xiàn)一個(gè)小電影[%s],目錄正在[%s]' %(f,cur_dir)) 123 ======================================================================== 124 125 for cur_dir,dirs,files in os.walk(r'E:/'): 126 print('當(dāng)前正在%s目錄下查找'%cur_dir) 127 for f in files: 128 if 'basttest' in f: #查詢某個(gè)關(guān)鍵字是否在文件中 129 real_path = os.path.join(cur_dir,f) #找到文件的真實(shí)路徑去刪除文件 130 os.remove(real_path) #刪除文件 131 # print('發(fā)現(xiàn)一個(gè)小電影[%s],目錄正在[%s]' %(f,cur_dir))

?

轉(zhuǎn)載于:https://www.cnblogs.com/baiby/p/10833741.html

總結(jié)

以上是生活随笔為你收集整理的os--模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。