Python編程新思維及實(shí)戰(zhàn)_第1頁(yè)
Python編程新思維及實(shí)戰(zhàn)_第2頁(yè)
Python編程新思維及實(shí)戰(zhàn)_第3頁(yè)
Python編程新思維及實(shí)戰(zhàn)_第4頁(yè)
Python編程新思維及實(shí)戰(zhàn)_第5頁(yè)
已閱讀5頁(yè),還剩31頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Python編程新思維及實(shí)戰(zhàn)

Python常用標(biāo)準(zhǔn)庫(kù)解析(中)

Python常用標(biāo)準(zhǔn)庫(kù)解析(中)

單元開(kāi)篇

X____________\__________/

式Python>123

單元開(kāi)篇

(1)os庫(kù)的使用⑵filecmp庫(kù)的使用

Python常用標(biāo)準(zhǔn)庫(kù)解析(中)

好Pythonk123。python

------------------------\

Python常用標(biāo)準(zhǔn)庫(kù)解析(中)

OS庫(kù)的使用

式Python>123

IOS庫(kù)介紹

os庫(kù)提供通用的、基本的操作系統(tǒng)交互功能

■S?G

WindowsMacOSLinux

?os庫(kù)提供了幾百個(gè)與文件系統(tǒng)、操作系統(tǒng)功能相關(guān)的函數(shù)

?常用路徑操作、進(jìn)程管理、環(huán)境參數(shù)等幾類

好Pythonk123。python

IOS庫(kù)介紹

os庫(kù)提供通用的、基本的操作系統(tǒng)交互功能

?路徑操作:os.path子庫(kù),處理文件路徑及信息

?進(jìn)程管理:?jiǎn)?dòng)系統(tǒng)中其他程序

?環(huán)境參數(shù):獲得系統(tǒng)軟硬件信息等環(huán)境參數(shù)

https://docs.python.Org/3.7/library/os.html

好Pythonk123。python

■os庫(kù)之路徑操作

os.path子庫(kù)的th為入口,用于操作和處理文件路徑

importos.path

importos.pathasop

好Pythonk123。python

|os庫(kù)之路徑操作

函數(shù)描述

返回path在當(dāng)前系統(tǒng)中的絕對(duì)路徑

os.path.abspath(path)>>>os.path.abspath("file.txt")

'C:\\Users\\TianSong\\Python36-32\\file.txt'

歸一化path的表示形式,統(tǒng)一用W份隔路徑

os.path.normpath(path)>>>os.path,normpath("D://PYE//file.txt")

'D:\\PYEWfile.txt'

好Pythonk123。python

|os庫(kù)之路徑操作

函數(shù)描述

返回當(dāng)前程序與文件之間的相對(duì)路徑(relativepath)

os.path.relpath(path)>>>os.path.relpath("C://PYE//file.txt")

,..w..w..w..w..w..WPYEWfile.txt'

返回palh中的目錄名稱

os.path.dirname(path)>>>os.path.dirname("D://PYE//file.txt")

'D:〃PYE'

好Pythonk123。python

os庫(kù)之路徑操作

函數(shù)描述

返回path中最后的文件名稱

os.path.basename(path)>>>os.path,basename("D://PYE//file.txt")

,file.txt'

組合path與paths,返回一個(gè)路徑字符串

os.path.join(path,*paths)>>>os.path.join("D:/'"PYE/file.txt")

'D:/PYE/file.txt'

好Pythonk123。python

|os庫(kù)之路徑操作

函數(shù)描述

判斷path對(duì)應(yīng)文件或目錄是否存在,返回True或False

os.path.exists(path)>>>os.path.exists("D://PYE//file.txt")

False

判斷path所對(duì)應(yīng)是否為已存在的文件,返回True或「alse

os.path.isfile(path)>>>os.path.isfile("D://PYE//file.txt")

True

好Pythonk123。python

|os庫(kù)之路徑操作

函數(shù)描述

判斷path所對(duì)應(yīng)是否為已存在的目錄,返回True或False

os.path.isdir(path)>>>os.path.isdir("D://PYE//file.txt")

False

返回path對(duì)應(yīng)文件或目錄上一次的訪問(wèn)時(shí)間

os.path.getatime(path)>>>os.path,getatime("D:/PYE/file.txt")

1518356633.7551725

好Pythonk123。python

|os庫(kù)之路徑操作

函數(shù)描述

返回path對(duì)應(yīng)文件或目錄最近一次的修改時(shí)間

os.path.getmtime(path)>>>os.path.getmtime("D:/PYE/file.txt")

1518356633.7551725

返回path對(duì)應(yīng)文件或目錄的創(chuàng)建時(shí)間

os.path.getctime(path)>>time.ctime(os.path.getctime("D:/PYE/file.txt"))

'SunFeb1121:43:532018,

好Pythonk123。python

os庫(kù)之路徑操作

函數(shù)描述

返回path對(duì)應(yīng)文件的大小,以字節(jié)為單位

os.path.getsize(path)>>>os.path.getsize("D:/PYE/file.txt")

180768

好Pythonk123。python

|os庫(kù)之路徑操作

os.path.exists(path)

os.path.abspath(path)

os.path.isfile(path)

os.path.normpath(path)

os.path.isdir(path)

os.path.relpath(path)

os.path.getatime(path)

os.path.dirname(path)

os.path.getmtime(path)

os.path.basename(path)

os.path.getctime(path)

os.path.join(path)

os.path.getsize(path)

好Python>123。python

|os庫(kù)之進(jìn)程管理

os.system(command)

?執(zhí)行程序或命令tommand

?在Windows系統(tǒng)中,返回值為and的調(diào)用返回信息

好Pythonk123。python

|os庫(kù)之進(jìn)程管理

cm

=程序員

importos

os?system(nC:\\Windows\\System32\\calc.exe")

>>>

0

好Pythonk123。python

|os庫(kù)之環(huán)境參數(shù)

獲取或改變系統(tǒng)環(huán)境信息

函數(shù)描述

修改當(dāng)前程序操作的路徑

os.chdir(path)

>?os.chdir("D:")

返回程序的當(dāng)前路徑

os.getcwdQ>>>os.getcwd()

'D:\\'

好Pythonk123。python

os庫(kù)之環(huán)境參數(shù)

獲取操作系統(tǒng)環(huán)境信息

函數(shù)描述

獲得當(dāng)前系統(tǒng)登錄用戶名稱

os.getlogin()>>>os.getlogin()

,TianSong'

獲得當(dāng)前系統(tǒng)的CPU數(shù)量

os.cpu_count()>>>os.cpu_count()

8

好Pythonk123。python

|os庫(kù)之環(huán)境參數(shù)

獲取操作系統(tǒng)環(huán)境信息

函數(shù)描述

獲得n個(gè)字節(jié)長(zhǎng)度的隨機(jī)字符串,通常用于加解密運(yùn)算

os.urandom(n)>>>os.urandom(10)

b'八xbe\xf2!\xcl=\x01gL\xb3'

好Pythonk123。python

■os庫(kù)小結(jié)

?路徑操作:os.path子庫(kù):abspath()^normpath。、relpath。、dirname。、

。、。、。、

basename()^joinexistsisfile()%isdir()%getatime

getmtime()^getctime()^getsize()

?進(jìn)程管理:os.system()

?環(huán)境參數(shù):os.chdir。、,s.getcwd()、os.getlogin。、os.cpu_count()^

os.urandom()

好Pythonk123。python

Python常用標(biāo)準(zhǔn)庫(kù)解析(中)

filecmp庫(kù)的使用

式Python>123

■filecmp庫(kù)介紹

filecmp庫(kù)提供比較目錄和文件的功能

?比較文件:用filecmp提供的函數(shù)

?比較目錄:用filecmp提供的dircmp類

?比較依據(jù):文件內(nèi)容、文件屬性信息等

https://docs.python.Org/3.7/library/filecmp.html

好Pythonk123。python

filecmp庫(kù)詳解

文件比較函數(shù)

函數(shù)描述

比較fl和f2兩個(gè)文件

filecmp.cmp(fl/f2,shallow=True)

當(dāng)shallow為T(mén)rue,比較os.stat。;否則比較文件內(nèi)容

比較dirl和dir2兩個(gè)目錄下的common文件

filecmp.cmpfiles(dirl,d\v2,當(dāng)shallow為T(mén)rue,比較os.statO;否則比較文件內(nèi)容

common,shallow=True)返回三個(gè)列表([],[],[])

包含匹配、不匹配和錯(cuò)誤的文件名

好Pythonk123。python

■filecmp庫(kù)詳解

z

文件比較函數(shù)

importfilecmp

filecmp.cmpfiles(nD:/AppH,"E:/App\[',abcN,Hdef/abcn])

比較:D:/App/abc和E:/App/abc

D:/App/def/abcftyApp/def/abc

好Pythonk123。python

■filecmp庫(kù)詳解

z

目錄比較

dcmp=filecmp.dircmpCdirl^dir2)

dcmp是一個(gè)對(duì)象,通過(guò)類的屬性來(lái)反應(yīng)目錄的比較情況

好Pythonk123。python

■filecmp庫(kù)詳解

目錄比較的類屬性

類屬性描述

left目錄dirl

right目錄dir2

leftjist目錄dirl的文件和子目錄列表

rightjist目錄dir2的文件和子目錄列表

好Pythonk123。python

■filecmp庫(kù)詳解

目錄比較的類屬性

類屬性描述

left_only僅在目錄dirl中的文件和子目錄列表

right_only僅在目錄dir2中的文件和子目錄列表

same_files目錄dirl和dir2中的相同文件

common同時(shí)在目錄dirl和dir2中的文件和子目錄列表

好Pythonk123。python

filecmp庫(kù)詳解

目錄比較的類屬性

類屬性描述

diff_files目錄dirl和dir2中都存在但卻不相同的文件

common_dirs同時(shí)在dirl和dir2中的子目錄列表

common_files同時(shí)在dirl和dir2中的文件列表

好Pythonk123。python

■filecmp庫(kù)小結(jié)

?文件比較:cmp().cmpfiles()

?目錄比較:dircmp類,其屬性包括:

left、right、right」ist、left_only、right.only^

same_files^common、diff_files、common_files^common_dirs

好Pythonk123。python

/-----------------------------------------X

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論