溫馨提示×

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

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

Python 3.6 中文件系統(tǒng)的操作方法

發(fā)布時(shí)間:2021-06-15 16:48:47 來源:億速云 閱讀:173 作者:chen 欄目:編程語言

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

Python 3.6 首次發(fā)布于 2016 年,盡管它已經(jīng)發(fā)布了一段時(shí)間,但它引入的許多特性都沒有得到充分利用,而且相當(dāng)酷。下面是其中的三個(gè)。

分隔數(shù)字常數(shù)

快回答哪個(gè)更大,10000000 還是 200000?你在看代碼時(shí)能正確回答嗎?根據(jù)當(dāng)?shù)氐牧?xí)慣,在寫作中,你會(huì)用 10,000,000 或  10.000.000 來表示第一個(gè)數(shù)字。問題是,Python 使用逗號(hào)和句號(hào)是用于其他地方。

幸運(yùn)的是,從 Python 3.6 開始,你可以使用下劃線來分隔數(shù)字。這在代碼中和使用字符串的 int() 轉(zhuǎn)換器時(shí)都可以使用:

import math math.log(10_000_000) / math.log(10)
7.0
math.log(int("10_000_000")) / math.log(10)
7.0

Tau 是對(duì)的

45 度角用弧度表示是多少?一個(gè)正確的答案是 π/4,但這有點(diǎn)難記。記住 45 度角是一個(gè)八分之一的轉(zhuǎn)角要容易得多。正如 Tau Manifesto  所解釋的,2π,稱為 Τ,是一個(gè)更自然的常數(shù)。

在 Python 3.6 及以后的版本中,你的數(shù)學(xué)代碼可以使用更直觀的常數(shù):

print("Tan of an eighth turn should be 1, got", round(math.tan(math.tau/8), 2)) print("Cos of an sixth turn should be 1/2, got", round(math.cos(math.tau/6), 2)) print("Sin of a quarter turn should be 1, go", round(math.sin(math.tau/4), 2))
Tan of an eighth turn should be 1, got 1.0 Cos of an sixth turn should be 1/2, got 0.5 Sin of a quarter turn should be 1, go 1.0

os.fspath

從 Python 3.6 開始,有一個(gè)神奇的方法表示“轉(zhuǎn)換為文件系統(tǒng)路徑”。當(dāng)給定一個(gè) str 或 bytes 時(shí),它返回輸入。

對(duì)于所有類型的對(duì)象,它尋找 __fspath__ 方法并調(diào)用它。這允許傳遞的對(duì)象是“帶有元數(shù)據(jù)的文件名”。

像 open() 或 stat 這樣的普通函數(shù)仍然能夠使用它們,只要 __fspath__ 返回正確的東西。

例如,這里有一個(gè)函數(shù)將一些數(shù)據(jù)寫入一個(gè)文件,然后檢查其大小。它還將文件名記錄到標(biāo)準(zhǔn)輸出,以便追蹤:

def write_and_test(filename):     print("writing into", filename)     with open(filename, "w") as fpout:         fpout.write("hello")     print("size of", filename, "is", os.path.getsize(filename))

你可以用你期望的方式來調(diào)用它,用一個(gè)字符串作為文件名:

write_and_test("plain.txt")
writing into plain.txt size of plain.txt is 5

然而,可以定義一個(gè)新的類,為文件名的字符串表示法添加信息。這樣可以使日志記錄更加詳細(xì),而不改變?cè)瓉淼墓δ埽?/p>

class DocumentedFileName:     def __init__(self, fname, why):         self.fname = fname         self.why = why     def __fspath__(self):         return self.fname     def __repr__(self):         return f"DocumentedFileName(fname={self.fname!r}, why={self.why!r})"

用 DocumentedFileName 實(shí)例作為輸入運(yùn)行該函數(shù),允許 open 和 os.getsize 函數(shù)繼續(xù)工作,同時(shí)增強(qiáng)日志:

write_and_test(DocumentedFileName("documented.txt", "because it's fun"))
writing into DocumentedFileName(fname='documented.txt', why="because it's fun") size of DocumentedFileName(fname='documented.txt', why="because it's fun") is 5

“ Python 3.6 中文件系統(tǒng)的操作方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

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

AI