溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python有哪些模塊

發布時間:2021-11-19 15:28:17 來源:億速云 閱讀:194 作者:iii 欄目:編程語言

本篇內容介紹了“Python有哪些模塊”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

1、os模塊 —— 文件操作系統:

os,經常裝系統的人可能經常會說,os鏡像,xxxx.os文件等等,其實os的意思是Operating System,也就是操作系統,既然是操作系統,操作文件,那應該跟node的fs差不多吧,不管怎樣,來看一下。
既然是操作系統和文件,那應該會有新建,修改,文件路徑,打開,關閉,讀取文件等等的基本操作,也會有文件權限,文件重命名等等的操作,就像node中的fs.read、fs.close、fs.path…,而Python中的os模塊,這些操作也是有的,它也是os.close,os.read,os.mkdir,os.open,os.rename等等。。。是不是發現也特別像JavaScript?

def test_os(self):
 print('os_start')
 print('當前絕對路徑為:' + os.path.abspath('./'))
 path = os.path.abspath('./')
 if not os.path.exists(path + '/hello'): # 判斷路徑是否存在
 os.mkdir(path + '/hello') # 建路徑
 
 # 類似JavaScript中的try catch,異常捕獲
 try:
 # 建文件,這種方式是默認的建文件方式,如txt是gbk的編碼格式,若需要儲存成另外編碼格式的,可以通過codecs模塊來創建文件
 # f = open(path + '/hello/test.txt', 'w')
 f = codecs.open(path + '/hello/test.txt', 'w', 'utf-8') # 建文件
 f.write('hello world, 你好啊, \n')
 f.close()
 except Exception as error:
 print('創建并寫入文件時報錯:' + str(error))
 print('當前工作目錄為:' + os.getcwd())
 # 文件重命名 —— rename / renames
 os.rename(path + '/hello/test.txt', path + '/hello/hello.txt') # 將之前的test.txt文件重命名為hello.txt文件
 print('os_end')

2、sys模塊 —— 系統指令與信息:

直接貼吧,主要是讀取信息

def test_sys(self):
 print(sys.argv) # 獲取命令行的參數,就比如我們剛剛執行了python ./package.py,這里就會在結果list里面返回,[程序名,argv0,1,2,...]
 
 # print(sys.modules) # 當前引入的模塊信息
 print(sys.platform) # 操作平臺信息
 print(sys.version) # python版本信息
 print(sys.copyright) # python的版權信息
 # print(sys.getswitchinterval()) # 線程切換間隔
 # print(sys.thread_info) # 當前線程信息
 # print(sys.stdin) # 輸入相關
 # print(sys.stdout) # 輸出相關
 # print(sys.stderr) # 錯誤相關
 # ...

3、requests模塊 —— http通訊:

既然requests是發http請求,處理通訊,按照我們的一般對于http或者更廣泛點的Tcp的理解,既然是請求,那就有get、post、put跟delete,實際上也是這樣的,requests.get,requests.post,requests.put,requests.delete,當然,還有一個綜合性的將get、post等等類型當做參數傳進去的requests.request。而且,注意,這是后臺,后臺,后臺!重要的事情說3遍,你不用再去管跨域,不存在跨域。。。記得有一次在閑聊的時候聊到前端接口調不通,我跟他提了跨域,然后有一次他后臺調不通了,他也以為是跨域,我用nginx,他也搞了一個nginx…

def test_requests(self):
 def getData():
 resp = requests.get('https://www.imooc.com/article/getarticlelist?marking=fe&page=4')
 return resp.content
 def requestData():
 resp = requests.request('GET', 'https://www.imooc.com/article/getarticlelist?marking=fe&page=4')
 return resp.content
 
 # requests.get
 # requests.post
 # requests.put
 # requests.delete
 
 # requests.request
 # 此外它還有,不常用的,可暫不理會
 # requests.head
 # requests.patch
 print(getData())
 print(requestData())

4、thread和threading模塊 —— 線程管理:

python通過thread跟threading來管理線程,先導入模塊

import _thread # thread與threading有沖突,python3在原有的基礎上增加了一個_私有前綴,不影響使用
import threading # threading更高級,也可以代替舊的thread,我們日常用threading就可以啦。

看看它是怎么用的:

def test_threading (self):
 # 開多條線程
 def run():
 print('當前執行的線程為:' + str(threading.current_thread()))
 time.sleep(2)
 thread_list = []
 i = 5
 while i > 0:
 t = threading.Thread(target=run)
 thread_list.append(t)
 i -= 1
 
 for t in thread_list:
 t.start()

5、time、datetime和calendar模塊 —— 日歷、時間:

看到上面的線程管理代碼時,里面我們很熟悉的sleep函數就來自于time模塊。獲取時間,這里就沒什么需要說的了,不過它本身附帶的格式化是比較好用的了,無需像JavaScript那樣去getFullYear()或去單獨實現格式化。

def test_time(self):
 print(time.time()) # 時間戳 
 print(time.localtime()) # 當地時間
 print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())) # 當地時間,格式化成帶星期幾格式
 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # 當地時間,并做格式化成2019-11-27 20:00:00
 print(datetime.date.today()) # 年月日
 print(datetime.date(2019, 11, 27)) # 年月日
 print(calendar.month(2019, 11)) # 生成日歷頁
 print(calendar.isleap(2020)) # 判斷傳入的年份是否為閏年
 print(calendar.month(2019, 11, w=3, l=2)) # 修改日歷行列間距,這里是3字符和2字符
 print('\ntime和datetime')

簡單看看輸出:

Python有哪些模塊

6、json模塊 —— 處理json:

def test_json(self):
 obj = {
 'a': 1,
 'b': 2
 }
 objStr = json.dumps(obj) # JavaScript里的JSON.stringify —— 序列化
 print(objStr)
 print(json.loads(objStr)) # JavaScript里的JSON.parse —— 解析

“Python有哪些模塊”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女