Python學習筆記十二_常用模組

來源:互聯網
上載者:User

標籤:dom   環境變數   div   wal   擷取檔案   重新命名   模組   不能   amp   

一、os
import os #對作業系統的一些操作print(os.getcwd())#取當前工作目錄os.chmod(‘/usr/local‘,7)#給linux檔案/目錄加許可權,對windows下不好使,1執行 2寫 4讀os.chdir(‘../‘)#更改目前的目錄(回到上一級目錄),沒有傳回值print(os.curdir)#目前的目錄, .print(os.pardir)#父目錄, ..os.makedirs(‘aa/bb‘)#遞迴建立檔案夾,父目錄不存在時建立,檔案夾已存在時,無法建立os.mkdir(‘cc‘)#建立檔案夾,檔案夾已存在時,無法建立os.removedirs(‘aa/bb‘)#遞迴刪除空目錄os.rmdir(‘cc‘)#刪除指定的空檔案夾os.remove(‘temp.py‘)#刪除檔案print(os.listdir())#列出目前的目錄下的所有檔案及檔案夾print(os.listdir(‘d:/‘))#列出d盤的所有檔案os.rename(‘test‘,‘test1‘)#重新命名print(os.stat(‘tmp.py‘))#擷取檔案資訊print(os.sep)#當前作業系統的路徑分隔字元,win:\,linux:/# day5+os.sep+tmp.py 用os.sep拼接路徑,提高系統相容性print(os.linesep)#當前作業系統的分行符號,\n \r\nprint(os.pathsep)#當前系統的環境變數中每個路徑的分隔字元,linux是:,windows是;print(os.environ)#當前系統的環境變數print(os.name)#當前系統名稱,Windows系統都是nt linux都是posix,用來判斷當前是什麼系統print(os.system(‘dir‘))#執行作業系統命令的,查看目前的目錄下有什麼內容dir,用來執行linux下備份res = os.system(‘dir‘)#不能擷取到命令執行結果,返回0代表命令執行成功,用來建立檔案夾等不需要返回結果的命令res = os.popen(‘ifconfig‘).read()#可以擷取到命令執行的結果os.open(‘top‘).read() #無法執行,因為top命令執行結果一直在動態變化os.popen(‘top -n 1‘).read() #只取一次,可以獲得結果print(os.path.abspath(‘.‘))#擷取絕對路徑print(__file__) # 擷取到當前檔案的絕對路徑,但路徑分隔字元不對print(os.path.abspath(__file__))# 擷取到當前檔案的絕對路徑print(os.path.split(‘/usr/hehe/hehe.txt‘))#分割路徑和檔案名稱,取list[1]就直接可以取出檔案名稱print(os.path.dirname(‘d:\\work‘))#擷取父目錄,擷取它的上一級目錄,d:\print(os.path.basename(‘d:\\work‘))#擷取最後一級,如果是檔案顯示檔案名稱,如果是目錄顯示目錄名,workprint(os.path.exists(‘d:\\work‘))#目錄/檔案是否存在,Trueprint(os.path.isabs(‘d:\\work‘))#判斷是否是絕對路徑,Trueprint(os.path.isfile(‘tmp.py‘))#判斷是否是一個檔案,1、檔案要存在2、必須是一個檔案print(os.path.isdir(‘d:\\work‘))#是否是一個路徑,目錄是否存在,Trueprint(os.path.getsize(‘tmp.py‘))#擷取檔案的大小,返回的是位元組,可以用來限制檔案上傳大小print(os.path.join(‘root‘,‘db‘,‘a.sql‘))#拼接成一個路徑,,分隔字元會自動識別,與os.sep的一致更智能,root\db\a.sqlprint(os.path.getatime(‘tmp.py‘))#輸出最近訪問時間print(os.path.getmtime(‘tmp.py‘))#輸出最近訪問時間

 os.walk是一個非常強大的方法,可以將檔案夾下的子檔案夾裡的檔案遍曆出來

os.walk返回的是一個二維數組,數組每個元素都包含絕對路徑、檔案夾、檔案三個元素,可以用三個變數來迴圈 

for data in os.walk(r‘d:\workspace\python\syz-dongrui\Day6‘):    print(data)for abs_path,dir,file in os.walk(r‘d:\workspace\python\syz-dongrui\Day6‘):#擷取目錄下內容    # 路徑前面加個r,不會把/n等轉義    # os.listdir()列出目前的目錄下的所有檔案,os.walk會遍曆子檔案夾找到所有檔案    print(abs_path,dir,file)    # abs_path 當前迴圈的絕對路徑    # dir 目錄下面所有的檔案夾 [ ]    #  file 目錄下面的所有檔案  []
二、sys三、random
import randomprint(random.randint(1,10))#隨機取整數print(random.randrange(1,20))#隨機產生一個rangeprint(random.random())#隨機浮點數,預設取0-1,不能指定範圍print(random.uniform(1,3))#隨機小數,可以指定範圍print(round(random.uniform(1,99),2))#隨機小數,保留小數點後兩位print(random.choice([1,2,3]))#隨機取1個元素print(random.sample(‘hello‘,3))#隨機取N個元素,返回的是listpickts = [‘A‘,‘J‘,‘Q‘,‘K‘,2,3,4,5,6]random.shuffle(pickts)#洗牌只能傳listprint(pickts)#[‘Q‘, ‘A‘, 2, 6, 3, 4, 5, ‘K‘, ‘J‘]
四、string
import stringprint(string.ascii_letters)#所有大小寫字母print(string.ascii_lowercase)#所有小寫字母print(string.ascii_uppercase)#所有大寫字母print(string.digits)#所有數字print(string.punctuation)#所有特殊標點符號print(string.printable)#數字+字母+特殊字元
五、time六、hashlib七、json

Python學習筆記十二_常用模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.