溫馨提示×

溫馨提示×

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

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

Python有哪些模塊

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

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

1、os模塊 —— 文件操作系統(tǒng):

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

def test_os(self):
 print('os_start')
 print('當(dāng)前絕對路徑為:' + 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模塊來創(chuàng)建文件
 # 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('創(chuàng)建并寫入文件時報錯:' + str(error))
 print('當(dāng)前工作目錄為:' + os.getcwd())
 # 文件重命名 —— rename / renames
 os.rename(path + '/hello/test.txt', path + '/hello/hello.txt') # 將之前的test.txt文件重命名為hello.txt文件
 print('os_end')

2、sys模塊 —— 系統(tǒng)指令與信息:

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

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

3、requests模塊 —— http通訊:

既然requests是發(fā)http請求,處理通訊,按照我們的一般對于http或者更廣泛點的Tcp的理解,既然是請求,那就有g(shù)et、post、put跟delete,實際上也是這樣的,requests.get,requests.post,requests.put,requests.delete,當(dāng)然,還有一個綜合性的將get、post等等類型當(dāng)做參數(shù)傳進去的requests.request。而且,注意,這是后臺,后臺,后臺!重要的事情說3遍,你不用再去管跨域,不存在跨域。。。記得有一次在閑聊的時候聊到前端接口調(diào)不通,我跟他提了跨域,然后有一次他后臺調(diào)不通了,他也以為是跨域,我用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來管理線程,先導(dǎo)入模塊

import _thread # thread與threading有沖突,python3在原有的基礎(chǔ)上增加了一個_私有前綴,不影響使用
import threading # threading更高級,也可以代替舊的thread,我們?nèi)粘S胻hreading就可以啦。

看看它是怎么用的:

def test_threading (self):
 # 開多條線程
 def run():
 print('當(dāng)前執(zhí)行的線程為:' + 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函數(shù)就來自于time模塊。獲取時間,這里就沒什么需要說的了,不過它本身附帶的格式化是比較好用的了,無需像JavaScript那樣去getFullYear()或去單獨實現(xiàn)格式化。

def test_time(self):
 print(time.time()) # 時間戳 
 print(time.localtime()) # 當(dāng)?shù)貢r間
 print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())) # 當(dāng)?shù)貢r間,格式化成帶星期幾格式
 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # 當(dāng)?shù)貢r間,并做格式化成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有哪些模塊”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI