Python3 從零單排8_time&random&shutil
阿新 • • 發佈:2018-12-05
time&datetime模組:
import time,datetime print(time.time())#獲取當前時間點的時間戳 print(time.localtime())#獲取當前時間點的時間元祖,如果傳了時間戳,將傳的時間戳裝換為時間元祖 print(time.strftime('%Y-%m-%d %H:%M:%S'))#獲取當前時間點的格式化時間 print(datetime.datetime.now())#獲取當前時間點的格式化時間,str(datetime.datetime.now())將時間多想轉成字串,還可以str(datetime.date())獲取年月日print(datetime.datetime.now()+datetime.timedelta(+3))#獲取當前時間點三天後的時間,還可以指定hours=3來更改時間 print(datetime.datetime.now()+datetime.timedelta(-3))#獲取當前時間點三天前的時間 print(time.gmtime(1234556489))#時間戳轉換成時間元祖 print(time.mktime(time.gmtime(1234556489)))#時間元祖轉換成時間戳 print(time.strftime('%Y',time.gmtime(1234556489)))#時間元祖轉換成格式化時間 print(time.strptime('2009','%Y'))#格式化時間轉換成時間元祖 def transfer(timestamp,format='%Y%m%d'):#時間戳格式化輸出 timetuple=time.gmtime(timestamp) print(time.strftime(format,timetuple)) def transfer2(timesformat,format='%Y%m%d'):#格式化時間轉換成時間戳輸出 timetuple=time.strptime(timesformat,format) print(time.mktime(timetuple)) transfer(231568465) transfer2('19770504')
random模組:
import random,string random.randrange(1,10) #返回1-10之間的一個隨機數,不包括10 random.randint(1,10) #返回1-10之間的一個隨機數,包括10 random.randrange(0, 100, 2) #隨機選取0到100間的偶數 random.random() #返回一個隨機浮點數 random.choice('abce3#[email protected]') #返回一個給定資料集合中的隨機字元 random.sample('abcdefghij',3) #從多個字元中選取特定數量的字元 lis = [i for i in range(5)] random.shuffle(lis) #打亂順序,輸出 lis = [2, 4, 1, 3, 0] 注意,shuffle直接打亂原list string.punctuation # '!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~' 特殊字元 string.digits # '0123456789' string.ascii_letters # 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' string.ascii_lowercase # 'abcdefghijklmnopqrstuvwxyz' string.ascii_uppercase # 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' string.hexdigits # '0123456789abcdefABCDEF' string.octdigits # '01234567' string.printable # '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~ \t\n\r\x0b\x0c' string.whitespace # ' \t\n\r\x0b\x0c'
shutil模組:
import shutil,zipfile,tarfile f1 = open("a.txt","r") f2 = open("b.txt","w") shutil.copyfileobj(f1,f2) #拷貝檔案物件 f2.flush() shutil.copyfile("a.txt","x.txt") #拷貝檔案內容 shutil.copy("a.txt","c.txt") #拷貝檔案內容和許可權 shutil.copy2("a.txt","c.txt") #拷貝檔案內容和狀態資訊 shutil.copytree("myproj","myproj_new") #拷貝整個目錄及目錄下的所有檔案(遞迴拷貝) shutil.copytree("myproj","myproj_new",ignore=shutil.ignore_patterns("*.txt")) #拷貝整個目錄,除了忽略的檔案 shutil.rmtree("myproj_new2") #刪除整個目錄及目錄下的所有檔案(遞迴刪除) shutil.move("myproj_new","myproj_new2") #剪下目錄 shutil.make_archive("myproj","zip","myproj") #壓縮shutil.make_archive("可以是路徑+壓縮檔名","zip;tar等","被壓縮檔案") 實質是呼叫了zipfile模組 #zipfile 壓縮zip 壓縮了檔案大小 z = zipfile.ZipFile("test.zip","w") z.write("a.txt") z.write("moudle_practice.py") z.write("myproj") #只壓縮了資料夾名,不會遞迴壓縮 z.close() #解壓 z = zipfile.ZipFile(r"E:\Study\Automantic\old_boy\module2\home_work\test.zip","r") z.extractall() z.close() # tarfile 打包 tar 檔案大小不會變 t = tarfile.open("your1.tar","w") t.add("a.txt") t.add("moudle_practice.py") t.add(r"E:\Study\Automantic\old_boy\module2\module\myproj\crm",arcname="crm1") #如果沒有指定arcname,那麼打的包裡會有E:\Study\Automantic\old_boy\module2\module\myproj\crm這麼多鞥路徑 t.close() # 解壓 t = tarfile.open("your.tar","r") t.extractall(r"E:\Study\Automantic\old_boy\module2\module\myproj") #可指定解壓地址 t.close()