av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

創(chuàng)新互聯(lián)Python教程:詳解Python標(biāo)準(zhǔn)庫(kù)

操作系統(tǒng)接口

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了孝義免費(fèi)建站歡迎大家使用!

os 模塊提供了大量和操作系統(tǒng)進(jìn)行交互的函數(shù):

>>> import os
>>> os.getcwd()      # 返回當(dāng)前工作路徑
'C:\\Python37'
>>> os.chdir('/server/accesslogs')   # 改變當(dāng)前工作路徑
>>> os.system('mkdir today')   # 調(diào)用系統(tǒng)shell自帶的mkdir命令
0

請(qǐng)確保使用 import os 而不是 from os import *。第二種方法會(huì)導(dǎo)致 os.open() 覆蓋系統(tǒng)自帶的 open() 函數(shù),這兩個(gè)函數(shù)的功能有很大的不同。

自帶的 dir() 和 help() 函數(shù)在使用大型模塊如 os 時(shí)能夠成為非常有用的交互工具:

>>> import os
>>> dir(os)
<返回一個(gè)包含os模塊所有函數(shù)的list>
>>> help(os)
<返回一個(gè)從os模塊docstring產(chǎn)生的手冊(cè)>

對(duì)于日常的文件或者目錄管理任務(wù),shutil 模塊提供了更高層次的接口,可以讓用戶更容易地使用:

>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
'archive.db'
>>> shutil.move('/build/executables', 'installdir')
'installdir'

文件通配符

glob 模塊提供了一個(gè)函數(shù),用于在目錄中進(jìn)行通配符搜索,得到一個(gè)文件列表。

>>> import glob
>>> glob.glob('*.py')
['primes.py', 'random.py', 'quote.py']

命令行參數(shù)

常見的工具類腳本經(jīng)常需要處理命令行參數(shù)。 這些參數(shù)儲(chǔ)存在 sys 模塊的 argv 屬性中,作為一個(gè)列表存在。例如,以下是在命令行運(yùn)行 python demo.py one two three 的結(jié)果輸出:

>>> import sys
>>> print(sys.argv)
['demo.py', 'one', 'two', 'three']

getopt 模塊使用 Unix 約定的 getopt() 函數(shù)處理 sys.argv 。更強(qiáng)大、靈活的命令行處理由 argparse 模塊提供。

錯(cuò)誤輸出重定向和退出程序

sys 模塊有 stdin,stdout 和 stderr 這些屬性。后者在處理警告和錯(cuò)誤信息時(shí)非常有用,就算 stdout 被重定向了,還是能看見錯(cuò)誤信息:

>>> sys.stderr.write('Warning, log file not found starting a new one\n')
Warning, log file not found starting a new one

退出程序最直接的方法是用 sys.exit()。

字符串匹配

re 模塊為字符串的進(jìn)階處理提供了正則表達(dá)式的工具。對(duì)于復(fù)雜的匹配操作,正則表達(dá)式給出了簡(jiǎn)潔有效的解決方案:

>>> import re
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
'cat in the hat'

當(dāng)只需要簡(jiǎn)單的功能時(shí),采用字符串的方法更簡(jiǎn)潔易懂:

>>> 'tea for too'.replace('too', 'two')
'tea for two'

數(shù)學(xué)庫(kù)

math 模塊可以訪問 C 語(yǔ)言編寫的浮點(diǎn)類型數(shù)學(xué)庫(kù)函數(shù):

>>> import math
>>> math.cos(math.pi / 4)0.70710678118654757
>>> math.log(1024, 2)10.0

 random 模塊提供了進(jìn)行隨機(jī)選擇的工具:

>>> import random
>>> random.choice(['apple', 'pear', 'banana'])
'apple'
>>> random.sample(range(100), 10)   # 不重復(fù)抽樣
[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
>>> random.random()    # 隨機(jī)的 float 類型輸出
0.17970987693706186
>>> random.randrange(6)    # 從 range(6) 的返回范圍內(nèi)產(chǎn)生隨機(jī)數(shù)
4

網(wǎng)絡(luò)請(qǐng)求

有一大堆模塊可以訪問網(wǎng)絡(luò)并根據(jù)各自網(wǎng)絡(luò)協(xié)議來處理數(shù)據(jù)。其中最簡(jiǎn)單的兩個(gè)分別是用于從 URL 獲取數(shù)據(jù)的 urllib.request 和用于發(fā)送郵件的 smtplib :

>>> from urllib.request import urlopen
>>> with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response:
...     for line in response:
...         line = line.decode('utf-8')  # 解碼.
...         if 'EST' in line or 'EDT' in line:  # 查看是否是EST或EDT時(shí)間
...             print(line)


Nov. 25, 09:43:32 PM EST >>> import smtplib >>> server = smtplib.SMTP('localhost') >>> server.sendmail('[email protected]', '[email protected]', ... """To: [email protected] ... From: [email protected] ... ... Beware the Ides of March. ... """) >>> server.quit()

日期和時(shí)間

datetime 模塊提供了多種用于簡(jiǎn)單處理和復(fù)雜處理日期和時(shí)間的類。支持日期時(shí)間的運(yùn)算、時(shí)間解析、格式化輸出等,實(shí)現(xiàn)上重點(diǎn)優(yōu)化了效率。模塊也支持了時(shí)區(qū)的概念。

>>> # 日期對(duì)象能非常方便的構(gòu)建和輸出
>>> from datetime import date
>>> now = date.today()
>>> now
datetime.date(2003, 12, 2)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'

>>> # 支持日期運(yùn)算
>>> birthday = date(1964, 7, 31)
>>> age = now - birthday
>>> age.days
14368

網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:詳解Python標(biāo)準(zhǔn)庫(kù)
網(wǎng)頁(yè)URL:http://uogjgqi.cn/article/dpjghsd.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流