從零開始的Python學習Episode 13——常用模組
阿新 • • 發佈:2018-10-31
模組
一、time模組
時間戳(timestamp) :時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量。
元組(struct_time) :struct_time元組共有9個元素共九個元素:(年,月,日,時,分,秒,一年中第幾周,一年中第幾天,夏令時)
import time #1 ()返回當前時間戳 print(time.time()/(365*24*60*60)+1970) #2018.8360429179454 #2 localtime(secs)將一個時間戳轉換為當前時區的struct_time,若secs引數為空,則以當前時間戳為準。 print(time.localtime()) #time.struct_time(tm_year=2018, tm_mon=10, tm_mday=21, tm_hour=11, tm_min=52, tm_sec=30, tm_wday=6, tm_yday=294, tm_isdst=0) #3 gmtime(secs)將一個時間戳轉換為UTC時區(0時區)的struct_time。 print(time.gmtime()) #time.struct_time(tm_year=2018, tm_mon=10, tm_mday=21, tm_hour=4, tm_min=6, tm_sec=11, tm_wday=6, tm_yday=294, tm_isdst=0)#4 mktime(t)將一個struct_time轉化為時間戳。 print(time.mktime(time.localtime())) #1540094878.0 #5 asctime([t])把一個表示時間的元組或者struct_time表示為這種形式:'Sun Jun 20 23:21:05 1993'。沒有引數則預設以time.time()作為引數。 print(time.asctime()) #Sun Oct 21 12:10:20 2018 #6 ctime([secs])把一個時間戳(按秒計算的浮點數)轉化為這種形式'Sun Jun 20 23:21:05 1993' print(time.ctime(time.time()))#Sun Oct 21 12:23:49 2018 #7 strftime(format[, t]) : 把一個代表時間的元組或者struct_time轉化為格式化的時間字串。如果t未指定,將傳入time.localtime() print(time.strftime("%Y-%m-%d %X",time.localtime())) #2018-10-21 12:27:01 #8 time.strptime(string[, format]把一個格式化時間字串轉化為struct_time。實際上它和strftime()是逆操作。 print(time.strptime('2018-10-21 12:27:01', '%Y-%m-%d %X')) #time.struct_time(tm_year=2018, tm_mon=10, tm_mday=21, tm_hour=12, tm_min=27, tm_sec=1, tm_wday=6, tm_yday=294, tm_isdst=-1)
二、random模組
import random #1 random()輸出0-1之間的隨機數 print(random.random()) #0.6563316163409958 #2 randint()輸出一個範圍內的隨機整數 print(random.randint(1,8)) #3 choice()輸出一個序列中的隨機一個元素 print(random.choice('hello')) print(random.choice(['hello',1,2,123])) #h 123 #4 sample()以列表形式輸出一個序列中的隨機幾個元素 print(random.sample(['hello',1,2,123],2)) #['hello', 2] #5 randrange()輸出一個範圍內的隨機整數(不含尾) print(random.randrange(1,3)) #2 #驗證碼 def v_code(): code = '' for i in range(5): add_num = random.choice([random.randrange(1,10),chr(random.randrange(65,91))]) code += str(add_num) print(code) v_code()
#N11QV
三、os模組
import os print(os.getcwd()) # 獲得當前工作目錄 print(os.chdir("dirname")) # 改變當前指令碼的工作路徑,相當於shell下的cd print(os.curdir) # 返回當前目錄‘.' print(os.pardir) # 獲取當前目錄的父目錄字串名‘..' print(os.makedirs('dirname1/dirname2')) # 可生成多層遞迴目錄 print(os.removedirs('dirname1/dirname2')) # 若目錄為空,則刪除,並遞迴到上一級目錄,如若也為空,則刪除,依此類推 print(os.mkdir('test4')) # 生成單級目錄;相當於shell中mkdir dirname print(os.rmdir('test4')) # 刪除單級空目錄,若目錄不為空則無法刪除,報錯;相當於shell中rmdir dirname print(os.listdir('/pythonStudy/s12/test')) # 列出指定目錄下的所有檔案和子目錄,包括隱藏檔案,並以列表方式列印 print(os.remove('log.log')) # 刪除一個指定的檔案 print(os.rename("oldname","newname")) # 重新命名檔案/目錄) print(os.stat('/pythonStudy/s12/test')) # 獲取檔案/目錄資訊 print(os.pathsep) # 輸出用於分割檔案路徑的字串';' print(os.name) # 輸出字串指示當前使用平臺。win->'nt'; Linux->'posix' print(os.system(command='bash')) # 執行shell命令,直接顯示 print(os.environ) # 獲得系統的環境變數 print(os.path.abspath('/pythonStudy/s12/test')) # 返回path規範化的絕對路徑 print(os.path.split('/pythonStudy/s12/test')) # 將path分割成目錄和檔名二元組返回 print(os.path.dirname('/pythonStudy/s12/test')) # 返回path的目錄。其實就是os.path.split(path)的第一個元素 print(os.path.basename('/pythonStudy/s12/test')) # 返回path最後的檔名。如果path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素 print(os.path.exists('test')) # 判斷path是否存在 print(os.path.isabs('/pythonStudy/s12/test')) # 如果path是絕對路徑,返回True print(os.path.isfile('test')) # 如果path是一個存在的檔案,返回True。否則返回False print(os.path.isdir('/pythonStudy/s12/test')) # 如果path是一個存在的目錄,則返回True。否則返回False print(os.path.getatime('/pythonStudy/s12/test')) # 返回path所指向的檔案或者目錄的最後存取時間 print(os.path.getmtime('/pythonStudy/s12/test')) # 返回path所指向的檔案或者目錄的最後修改時間
四、sys模組
import sys print(sys.argv) # 命令列引數List,第一個元素是程式本身路徑 print(sys.exit(n)) # 退出程式,正常退出時exit(0) print(sys.version) # 獲取python的版本資訊 print(sys.path) # 返回模組的搜尋路徑,初始化時使用PYTHONPATH環境變數的值 print(sys.platform) # 返回操作平臺的名稱