標籤:shell 函數 不為 exist 元組 none ftime 方法 div
可命名元祖namedtuple
namedtuple樣本
deque雙端隊列
deque樣本
隊列queue
queue樣本
有序的字典(key是保持順序的)OrderedDict
OrderedDict樣本
使用字典時,如果key不存在時,返回一個預設值,可以使用defaultdict
defaultdict樣本
Counter計數,它是一個無序的容器類型,以字典的索引值對形式儲存,其中元素作為key,其計數作為value。計數值可以是任意的Interger(包括0和負數)
Counter樣本
時間模組time
time模組
import time# ret = time.time() #時間戳記,時間戳記表示的是從1970年1月1日00:00:00開始按秒計算的位移量# print(ret) #1510647685.4264479# print(type(ret))# 格式化的時間字串strftime# ret = time.strftime(‘%Y-%m-%d %A %H-%M-%S‘)# print(ret)# ret = time.localtime(1510647685.4264479)# print(ret)# 元組(struct_time):struct_time元組共有9個元素共九個元素:(年,月,日,時,分,秒,一年中第幾周,一年中第幾天等)# s = time.time()# # print(s)# ret = time.struct_time(s)# print(ret)# ret = time.gmtime()# print(ret)# ret1 = time.localtime()# print(ret1)# print(time.gmtime(1222222222) )# print(time.localtime(1222222222) )# print(time.localtime())# ret = time.localtime(1222222222)# print(ret)# print(time.mktime(ret))# ret = time.strftime(‘%Y-%m-%d‘)# print(ret)# ret1 = time.strptime("2017-03-16","%Y-%m-%d")# print(ret1)# # ret2 = time.mktime(ret1)# # print(ret2)## ret2 = time.strftime(‘%Y-%m‘,ret1)# print(ret2)# true_time=time.mktime(time.strptime(‘2017-09-11 08:30:00‘,‘%Y-%m-%d %H:%M:%S‘))# time_now=time.mktime(time.strptime(‘2017-09-12 11:00:00‘,‘%Y-%m-%d %H:%M:%S‘))# dif_time=time_now-true_time# struct_time=time.gmtime(dif_time)# print(struct_time)# print(‘過去了%d年%d月%d天%d小時%d分鐘%d秒‘%(struct_time.tm_year-1970,struct_time.tm_mon-1,# struct_time.tm_mday-1,struct_time.tm_hour,# struct_time.tm_min,struct_time.tm_sec))# ret = time.asctime(time.gmtime())# print(ret)# ret1 = time.ctime(2000000000)# print(ret1)
sys模組
sys
random模組
random講解及樣本
# 隨機小數# print(random.random()) #0-1之間的小數# print(random.uniform(10,20)) #n,m之間的小數# # 產生隨機整數# print(random.randint(1,2))# print(random.randrange(2))# print(random.randrange(1,2))# print(random.randrange(1,5,2))# # 從一個序列中隨機播放:一個choice,多個sample# 隨機播放一個返回choice# print(random.choice(1,2,3)) #1或者2或者3#隨機播放多個返回,返回的個數為函數的第二個參數sample# print(random.sample([1,25,[1,2]],2)) #列表元素任意2個組合# 打亂一個序列的順序shuffle# item = [1,3,5,7,9]# random.shuffle(item) #改變了原列表# print(item) #應用:洗牌,抽獎,測試一些排序演算法# 練習:產生隨機驗證碼# 方法一# print(random.randint(100000,999999))# 方法二:# print(random.randrange(100000,1000000))# 方法三# l = []# for i in range(6):# num = random.randint(0,9)# l.append(str(num))# print(‘‘.join(l))# 產生一個6位元字隨機驗證碼。不能有重複,會少了好多種可能# print(random.sample(range(0,10),6))# l = []# for i in range(6):# alpha = chr(random.randint(65,90))# num = str(random.randint(0,9))# ret = random.choice([alpha,num])# l.append(ret)# print(‘‘.join(l))
os模組
os模組是與作業系統互動的一個介面
os相關
‘‘‘os.getcwd() 擷取當前工作目錄,即當前python指令碼工作的目錄路徑os.chdir("dirname") 改變當前指令碼工作目錄;相當於shell下cdos.curdir 返回目前的目錄: (‘.‘)os.pardir 擷取目前的目錄的父目錄字元串名:(‘..‘)os.makedirs(‘dirname1/dirname2‘) 可產生多層遞迴目錄os.removedirs(‘dirname1‘) 若目錄為空白,則刪除,並遞迴到上一級目錄,如若也為空白,則刪除,依此類推os.mkdir(‘dirname‘) 產生單級目錄;相當於shell中mkdir dirnameos.rmdir(‘dirname‘) 刪除單級空目錄,若目錄不為空白則無法刪除,報錯;相當於shell中rmdir dirnameos.listdir(‘dirname‘) 列出指定目錄下的所有檔案和子目錄,包括隱藏檔案,並以列表方式列印os.remove() 刪除一個檔案os.rename("oldname","newname") 重新命名檔案/目錄os.stat(‘path/filename‘) 擷取檔案/目錄資訊os.sep 輸出作業系統特定的路徑分隔字元,win下為"\\",Linux下為"/"os.linesep 輸出當前平台使用的行終止符,win下為"\t\n",Linux下為"\n"os.pathsep 輸出用於分割檔案路徑的字串 win下為;,Linux下為:os.name 輸出字串指示當前使用平台。win->‘nt‘; Linux->‘posix‘os.system("bash command") 運行shell命令,直接顯示os.popen("bash command) 運行shell命令,擷取執行結果os.environ 擷取系統內容變數os.pathos.path.abspath(path) 返回path正常化的絕對路徑 os.path.split(path) 將path分割成目錄和檔案名稱二元組返回 os.path.dirname(path) 返回path的目錄。其實就是os.path.split(path)的第一個元素 os.path.basename(path) 返回path最後的檔案名稱。如何path以/或\結尾,那麼就會返回空值。 即os.path.split(path)的第二個元素os.path.exists(path) 如果path存在,返回True;如果path不存在,返回Falseos.path.isabs(path) 如果path是絕對路徑,返回Trueos.path.isfile(path) 如果path是一個存在的檔案,返回True。否則返回Falseos.path.isdir(path) 如果path是一個存在的目錄,則返回True。否則返回Falseos.path.join(path1[, path2[, ...]]) 將多個路徑組合後返回,第一個絕對路徑之前的參數將被忽略os.path.getatime(path) 返回path所指向的檔案或者目錄的最後訪問時間os.path.getmtime(path) 返回path所指向的檔案或者目錄的最後修改時間os.path.getsize(path) 返回path的大小‘‘‘注意:os.stat(‘path/filename‘) 擷取檔案/目錄資訊 的結構說明stat 結構:st_mode: inode 保護模式st_ino: inode 節點號。st_dev: inode 駐留的裝置。st_nlink: inode 的連結數。st_uid: 所有者的使用者ID。st_gid: 所有者的組ID。st_size: 普通檔案以位元組為單位的大小;包含等待某些特殊檔案的資料。st_atime: 上次訪問的時間。st_mtime: 最後一次修改的時間。st_ctime: 由作業系統報告的"ctime"。在某些系統上(如Unix)是最新的中繼資料更改的時間,在其它系統上(如Windows)是建立時間(詳細資料參見平台的文檔)。
python-常用模組