一 collections模塊python
1 orderedDict 有序字典shell
1 # d = collections.OrderedDict() 2 # 3 # d['蘋果'] = 10 4 # d['手機']=5000 5 # print(d) 6 # for i in d: 7 # print(i,d[i])
2 defaultdict 默認字典app
例子:小於66的放到k2,大於66的放到k1,造成一個新字典dom
l= [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
1 # new_dict = defaultdict(list) 2 # for value in l: 3 # if value > 66: 4 # new_dict['k1'].append(value) 5 # else: 6 # new_dict['k2'].append(value) 7 # print(new_dict)
3 namedtuple 生成能夠使用名字來訪問元素內容的tuple函數
1 # from collections import namedtuple 2 # Point = namedtuple('point',['x','y']) 3 # p=Point(1,2) 4 # print(p.x) 5 # print(p.y)
4 deque雙端隊列spa
1 # from collections import deque 2 # q=deque() 3 # print(q) 4 # q.append(1) 5 # q.append(2) 6 # print(q) 7 # q.pop() 8 # print(q) 9 # q.appendleft('a')左添加 10 # q.appendleft('b') 11 # print(q) 12 # q.popleft()左刪除 13 # print(q)
二time模塊3d
1三種時間格式code
1 import time 2 print(time.time()) ##時間戳時間 3 print(time.strftime('%Y-%m-%d %H:%M:%S'))##字符串時間 4 print(time.localtime()) ##結構化時間
2 時間轉換orm
1 時間戳---> 結構化時間-->字符串時間 2 # print(time.localtime(1600000000)) ##時間戳轉換成結構化時間 3 # struct_time=time.gmtime(1600000000) ##結構化 4 # print(time.strftime('%Y-%m-%d %H:%M:%S',struct_time))##結構化轉換成字符串 5 6 結果是time.struct_time(tm_year=2020, tm_mon=9, tm_mday=13, tm_hour=20, tm_min=26, tm_sec=40, tm_wday=6, tm_yday=257, tm_isdst=0) 7 2020-09-13 12:26:40 8 9 字符串時間-->結構化時間-->時間戳時間 10 11 12 # s = '2015-12-3 8:30:20' 13 # ret = time.strptime(s,'%Y-%m-%d %H:%M:%S') ##字符串轉換成結構化 14 # print(ret) 15 # print(time.mktime(ret)) ##結構化轉換成時間戳 16 17 結果是: 18 time.struct_time(tm_year=2015, tm_mon=12, tm_mday=3, tm_hour=8, tm_min=30, tm_sec=20, tm_wday=3, tm_yday=337, tm_isdst=-1) 19 1449102620.0
3 計算時間差blog
1 ##計算如今的時間到1991相差了多少 2 ago_time='1991-1-3' 3 now_time=time.localtime() 4 current_time=time.strptime(ago_time,'%Y-%m-%d') 8 print('過去了%d年%d月%d天' 9 %(now_time.tm_year-current_time.tm_year,now_time.tm_mon-current_time.tm_mon,now_time.tm_mday-current_time.tm_mday))
三random模塊 (能夠用於隨機生成可用於驗證碼)
1
1 import random 2 # print(random.random()) ##是介於0和1之間的小數 3 # print(random.uniform(1,4)) ##介於1和4之間的的小數 4 # print(random.randint(1,5)) ##介於1和5之間的隨機整數
2例子隨機生成一個驗證碼
1 #(65,90)A-Z (97,122)a-z 2 import random 3 yanzheng='' 4 for i in range(6):##6位驗證碼 5 num1=random.randint(65,90)##大寫字母 6 alpha1=chr(num1) 7 num2=random.randint(97,122) ##小寫字母 8 alpha2=chr(num2) 9 num3=str(random.randint(0,9))##數字 10 #print(alpha1,alpha2,num3) 11 s=random.choice([alpha1,alpha2,num3]) 12 yanzheng+=s 13 print(yanzheng)
四 sys模塊 (sys模塊是與python解釋器交互的一個接口)
1 import sys 2 #sys.exit() ##解釋器退出,程序結束 3 # print(sys.path) #一個模塊是否可以被導入 全看在不在sys.path列表所包含的路徑下 4 # print(sys.modules) ## 放了全部在解釋器運行的過程當中導入的模塊名 5 # print(sys.argv) ##返回['D:/python21/day5/模塊.py'] 6 7 8 下面是一個傳參數的例子 9 10 if sys.argv[1]=='hu' and sys.argv[2]=='123': 11 print('登陸成功') 12 else: 13 sys.exit() 14 ##注意,不能在pycharm裏面直接執行,應該是D:\python21\day5\模塊.py這種執行方式
五 os模塊
1
1 import os 2 # print(os.getcwdb())#獲取當前目錄 3 # os.chdir('C:\python21') #更改目錄 4 # print(os.getcwdb()) 5 6 7 print(os.name) #輸出字符串指示當前使用平臺。win->'nt'; Linux->'posix' 8 #print(os.makedirs(r'c:/a/b')) ##建立多級目錄 9 #os.removedirs(r'c:/a/b')# 若目錄爲空,則刪除,並遞歸到上一級目錄,如若也爲空,則刪除,依此類推 10 #print(os.listdir(r'c:\python21')) ##列出當前目錄全部的文件和目錄,以列表形式打印 11 # print(os.environ) ##獲取系統環境變量 12 13 #os.system("dir") ##運行shell命令,直接顯示,不用打印 14 # ret = os.popen("dir").read() ##運行shell命令,獲取執行結果,須要打印 15 # print(ret)
2 os.path
1 # print(os.path.abspath(r'模塊.py')) ##獲取絕對路徑 2 # #結果C:\python21\day5\模塊.py 3 # print(os.path.dirname('C:\python21\day5\模塊.py')) ##獲取上一級目錄 4 # #結果C:\python21\day5 5 # print(os.path.split('C:\python21\day5\模塊.py'))#將path分割成目錄和文件名二元組返回 6 # #結果('C:\\python21\\day5', '模塊.py') 7 # print(os.path.basename('C:\python21\day5\模塊.py')) ##只獲取到文件名 8 #結果:模塊.py 9 # print(os.path.exists('C:\python21\day5\模塊.py'))##判斷路徑是否存在,存在返回true 10 # print(os.path.isabs(r'模塊.py')) ##判斷是不是絕對路徑,是返回true 11 # print(os.path.isfile('C:\python21\day5')) ##判斷是不是文件,是返回true 12 # print(os.path.isdir('C:\python21\day5')) ##判斷是不是目錄,是返回true 13 # print(os.path.join('c:','python21'))##將多個路徑組合後返回 14 # print(os.path.getatime('C:\python21\day5')) #獲取文件目錄最後訪問時間 15 # print(os.path.getmtime('C:\python21\day5')) #獲取文件目錄最後修改時間 16 # print(os.path.getsize(r'C:\python21\day5\模塊.py'))#獲取文件大小 17 # print(os.path.getsize(r'C:\python21\day4'))#獲取目錄大小
3 例子計算目錄下文件的大小
第一種狀況,計算的目錄不在當前目錄
1 # dirs = "C:\python21\day4" 2 # sum=0 3 # C:\python21\day4\ceshi.py 4 # for path in ret: 5 # if os.path.isfile(os.path.join(dirs, path)): 6 # sum+=os.path.getsize(os.path.join(dirs,path)) 7 # print(sum)
第二種狀況,計算當前目錄文件大小
1 # ret = os.listdir(r'C:\python21\day5') 2 # for path in ret: 3 # if os.path.isfile(path): 4 # sum+=os.path.getsize(path) 5 # print(sum)
第三中狀況,計算文件夾下的文件夾及其文件大小
1 sum=0 2 def func(dirs): # 'C:\python21\day5' ##默認是字符串 3 global sum 4 for file in os.listdir(dirs):##列出dirs目錄下面全部的文件及其目錄 5 if os.path.isdir(os.path.join(dirs,file)): 6 7 func(os.path.join(dirs,file))##若是是文件夾則繼續執行函數func 8 else: 9 print("%s:%s" % (file,os.path.getsize(os.path.join(dirs,file)))) 10 sum+=os.path.getsize(os.path.join(dirs,file)) 11 return sum 12 print(func(r'C:\python21\day5'))
# dirs 是 C:\python21\day5\a # file是 目錄下面的文件 C:\python21\day5\a\b.txt