1. 程式人生 > >6、【Python】一些常用模組

6、【Python】一些常用模組

python 常用模組

1、Time模組
import time

print (time.time())#返回時間戳

print(time.localtime())#返回本地時間

print (time.gmtime()) #返回標準時間(格林威治時間)
#將本地時間格式化輸出
print (time.strftime('%Y-%m-%d %H:%M:%S, time.localtime()))
2、DataTime模組
import datetime

print(datetime.datetime.now())  # 返回當前本地時間

print(datetime.
datetime.now() + datetime.timedelta(days=-3)) # 當前時間減三天 print(datetime.datetime.now() + datetime.timedelta(days=3)) # 當前時間加三天 print(datetime.datetime.now() + datetime.timedelta(hours=-3)) # 當前時間減三小時 print(datetime.datetime.now() + datetime.timedelta(hours=3)) # 當前時間加三小時 print(datetime.datetime.
now() + datetime.timedelta(minutes=-3)) # 當前時間減三分鐘 print(datetime.datetime.now() + datetime.timedelta(minutes=3)) # 當前時間加三分鐘 print(datetime.datetime.now() + datetime.timedelta(seconds=-3)) # 當前時間減三秒 print(datetime.datetime.now() + datetime.timedelta(seconds=3)) # 當前時間加三秒 print(datetime.datetime.
now() + datetime.timedelta(milliseconds=-3)) # 當前時間減三毫秒 print(datetime.datetime.now() + datetime.timedelta(milliseconds=3)) # 當前時間加三毫秒 print(datetime.datetime.now().replace(year=2019)) # 替換當前時間
3、random 模組
import random

print (random.random())#隨機輸出(0,1)之間的小數
print (random.randint())#隨機輸出(0,9)之間的整數
print (random.randrange(0, 2))#隨機輸出[0, 2)之間的整數
print (random.choice('abcdefg'))#隨機選擇字串中的一個字元
print (random.sample('fsdgsdfbgdf', 5))#從字串中隨機選出指定的長度
4、os模組
import os

print (os.getcwd()) #返回當前檔案的工作目錄
print (os.chdir()) #改變當前指令碼工作目錄
print (os.curdir()) #返回當前目錄(.)
print (os.pardir()) #返回當前目錄的父目錄(..)
os.mkdir('a') #建立新目錄a
os.rmdir('a') #刪除目錄a
os.makedirs('a/b/c/c/d') #遞迴生成多級目錄
os.removedirs('a/b/c/d') #遞迴刪除多級目錄
print (os.listdir(os.path.dirname(os.path.abspath(__file__)))) #列出檔案下所有檔案
os.rename(os.path.abspath(__file__), '模組.py') #重新命名檔案
print (os.stat(os.path.abspath(__file__))) #列出檔案資訊
print (os.sep) #列出作業系統的檔案分割符
print (os.linesep) #列出當前作業系統的行終止符(換行符)
print (os.pathsep) #輸出用於分割檔案路徑的字串(:)
os.system('dir') #執行作業系統命令
print (os.environ) #返回作業系統的環境變數
print (os.path.abspath(__file__)) #返回當前檔案的絕對路徑
print (os.path.split(os.path.abspath(__file__))) #將path分割成路徑和檔名
print (os.path.dirname(os.path.abspath(__file__))) #返回檔案上級路徑
print (os.path.basename(os.path.abspath(__file__))) #返回檔名
print (os.path.isdir(os.path.dirname(os.path.abspath(__file__)))) #判斷是否是一個資料夾
print (os.path.isfile(os.path.abspath(__file__))) #判斷是否是一個檔案
print (os.path.isabs(os.path.abspath(__file__))) #返回檔案的絕對路徑
print (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getatime(os.path.abspath(__file__))))) #返回檔案的最後存取時間
print (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(os.path.abspath(__file__))))) #返回檔案最後修改時間
5、shutil 模組
shutil.copyfile(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + os.sep + '字型檔', '字型檔') #copy檔案
os.makedirs('a/b/c/d/e/f/g')
shutil.rmtree('a') #刪除檔案
shutil.copytree('a', 'c') #拷貝檔案
6、zipfile 模組
z = zipfile.Zipfile(file='new_zip.zip', mode='w') #壓縮檔案
z.write('a/b/')
z.close()

z = zipfile.Zipfile(file='new_zip.zip', mode='r')#解壓檔案
z.extractall(path=os.path.dirname(os.path.abspath(__file__)))
z.close()
7、json 模組

    dumps函式的作用是將字典型別的資料轉換為json格式的字串,loads函式的作用是將json格式的字串轉換成字典型別的資料。

import json

json_data = {'name' : 'nick', 'age' : 22, 'sex' : 'male'}

json_str = json.dumps(json_data) #將字典型別的資料轉換成json字串
print (json_str)
print (tpye(json_str))

json_load = json.loads(json_str)
print (json_loa
print (type(json_load))

    dump和load操作都與檔案有關,dump是將字典型別的資料轉換成json格式的字串並寫入檔案之中,而load函式的作用是將檔案中json格式的字串讀出並轉換成字典。

import json

json_data = {'name' : 'nick', 'age' : 22, 'sex' : 'male'}

with open(file='json_txt', ehcoding='utf-8', mode='w') as f:
    json.dump(json_data, f)

with open(file='json_txt', encoding='utf-8', mode='r') as f:
    print (json.load(f))