參考資料:https://www.cnblogs.com/yuanchenqi/articles/5732581.html
1 time 模塊
- 時(shí)間表示的三種方式:
(1)時(shí)間戳timestamp :時(shí)間戳表示的是從1970年1月1日00:00:00開始按秒計(jì)算的偏移量崭孤。運(yùn)行“type(time.time())”溜族,返回的是float類型。
(2)格式化的時(shí)間字符串
(3)元祖類型struct_time : struct_time元組共有9個(gè)元素:(年牍颈,月榄棵,日鳍徽,時(shí)曲伊,分髓梅,秒织阅,一年中第幾周壳繁,一年中第幾天,夏令時(shí)) -
時(shí)間轉(zhuǎn)換圖
import time
#當(dāng)前時(shí)間戳
print(time.time()) # 1531915796.7699454
#當(dāng)前時(shí)間的結(jié)構(gòu)化時(shí)間
print(time.localtime()) #time.struct_time(tm_year=2018, tm_mon=7, tm_mday=18, tm_hour=20, tm_min=10, tm_sec=20, tm_wday=2, tm_yday=199, tm_isdst=0)
t=time.localtime()
print(t.tm_year)
print(t.tm_mon)
#將時(shí)間戳轉(zhuǎn)化為本地結(jié)構(gòu)化時(shí)間
print(time.localtime(1531915796.7699454 )) #time.struct_time(tm_year=2018, tm_mon=7, tm_mday=18, tm_hour=20, tm_min=9, tm_sec=56, tm_wday=2, tm_yday=199, tm_isdst=0)
#將時(shí)間戳轉(zhuǎn)化為utc結(jié)構(gòu)化時(shí)間
print(time.gmtime(1531915796.7699454)) #time.struct_time(tm_year=2018, tm_mon=7, tm_mday=18, tm_hour=12, tm_min=9, tm_sec=56, tm_wday=2, tm_yday=199, tm_isdst=0)
#將結(jié)構(gòu)化時(shí)間轉(zhuǎn)換成時(shí)間戳
print(time.mktime(time.localtime())) #1531916292.0
#將結(jié)構(gòu)化時(shí)間轉(zhuǎn)換成指定結(jié)構(gòu)的字符串
print(time.strftime("%Y-%m-%d %X",time.localtime())) #2018-07-19 11:40:29
#將字符串轉(zhuǎn)換成結(jié)構(gòu)化時(shí)間
print(time.strptime("2016:12:24 17:50:36","%Y:%m:%d %X")) #time.struct_time(tm_year=2016, tm_mon=12, tm_mday=24, tm_hour=17, tm_min=50, tm_sec=36, tm_wday=5, tm_yday=359, tm_isdst=-1)
print(time.asctime()) #Wed Jul 18 20:11:18 2018
import datetime
print(datetime.datetime.now()) #2018-07-19 11:55:59.867921
2 random模塊
import random
print(random.random()) #0.5885584628653848 (0,1)的float類型
print(random.randint(1,3)) #[1,3]隨機(jī)整數(shù)
print(random.randrange(1,3)) #[1,3) 隨機(jī)整數(shù)
print(random.choice([1,'23',[4,5],'hee'])) #列表中的隨機(jī)數(shù)
print(random.sample([1,'2','ht'],2)) #2表示選擇2個(gè)元素
print(random.uniform(1,3)) #1.6303399953822733
item=[1,3,5,7,9]
random.shuffle(item) #隨機(jī)排序
print(item) #[9, 1, 5, 3, 7]
- 用random實(shí)現(xiàn)驗(yàn)證碼示例
def v_code():
code = ''
for i in range(5):
num = random.randint(0, 9) #數(shù)字
alf = chr(random.randint(65, 90)) #大小寫字母
add = random.choice([num, alf]) #用于拼接驗(yàn)證碼的字符
code += str(add) #拼接5次荔棉,即完成一個(gè)驗(yàn)證碼
return code
print(v_code()) #497YL
3 os模塊
os模塊是與操作系統(tǒng)交互的一個(gè)接口
os.getcwd() 獲取當(dāng)前工作目錄闹炉,即當(dāng)前python腳本工作的目錄路徑
os.chdir("dirname") 改變當(dāng)前腳本工作目錄;相當(dāng)于shell下cd
os.curdir 返回當(dāng)前目錄: ('.')
os.pardir 獲取當(dāng)前目錄的父目錄字符串名:('..')
os.makedirs('dirname1/dirname2') 可生成多層遞歸目錄
os.removedirs('dirname1') 若目錄為空润樱,則刪除渣触,并遞歸到上一級(jí)目錄,如若也為空壹若,則刪除嗅钻,依此類推
os.mkdir('dirname') 生成單級(jí)目錄;相當(dāng)于shell中mkdir dirname
os.rmdir('dirname') 刪除單級(jí)空目錄店展,若目錄不為空則無法刪除养篓,報(bào)錯(cuò);相當(dāng)于shell中rmdir dirname
os.listdir('dirname') 列出指定目錄下的所有文件和子目錄赂蕴,包括隱藏文件柳弄,并以列表方式打印
os.remove() 刪除一個(gè)文件
os.rename("oldname","newname") 重命名文件/目錄
os.stat('path/filename') 獲取文件/目錄信息
os.sep 輸出操作系統(tǒng)特定的路徑分隔符,win下為"\\",Linux下為"/"
os.linesep 輸出當(dāng)前平臺(tái)使用的行終止符概说,win下為"\t\n",Linux下為"\n"
os.pathsep 輸出用于分割文件路徑的字符串 win下為;,Linux下為:
os.name 輸出字符串指示當(dāng)前使用平臺(tái)碧注。win->'nt'; Linux->'posix'
os.system("bash command") 運(yùn)行shell命令,直接顯示
os.environ 獲取系統(tǒng)環(huán)境變量
os.path.abspath(path) 返回path規(guī)范化的絕對(duì)路徑
os.path.split(path) 將path分割成目錄和文件名二元組返回
os.path.dirname(path) 返回path的目錄糖赔。其實(shí)就是os.path.split(path)的第一個(gè)元素
os.path.basename(path) 返回path最后的文件名萍丐。如何path以/或\結(jié)尾,那么就會(huì)返回空值挂捻。即os.path.split(path)的第二個(gè)元素
os.path.exists(path) 如果path存在碉纺,返回True船万;如果path不存在刻撒,返回False
os.path.isabs(path) 如果path是絕對(duì)路徑,返回True
os.path.isfile(path) 如果path是一個(gè)存在的文件耿导,返回True声怔。否則返回False
os.path.isdir(path) 如果path是一個(gè)存在的目錄,則返回True舱呻。否則返回False
os.path.join(path1[, path2[, ...]]) 將多個(gè)路徑組合后返回醋火,第一個(gè)絕對(duì)路徑之前的參數(shù)將被忽略
os.path.getatime(path) 返回path所指向的文件或者目錄的最后存取時(shí)間
os.path.getmtime(path) 返回path所指向的文件或者目錄的最后修改時(shí)間
os.path常用操作的參考資料:https://blog.csdn.net/use_my_heart/article/details/52353756
4 sys模塊
sys.argv 命令行參數(shù)List悠汽,第一個(gè)元素是程序本身路徑
sys.exit(n) 退出程序,正常退出時(shí)exit(0)
sys.version 獲取Python解釋程序的版本信息
sys.maxint 最大的Int值
sys.path 返回模塊的搜索路徑芥驳,初始化時(shí)使用PYTHONPATH環(huán)境變量的值
sys.platform 返回操作系統(tǒng)平臺(tái)名稱
#進(jìn)度條效果示例:
import sys,time
for i in range(50):
sys.stdout.write('*')
time.sleep(0.1)
sys.stdout.flush()