第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在python中第二次讀取文件時(shí)出現(xiàn)問題

在python中第二次讀取文件時(shí)出現(xiàn)問題

瀟湘沐 2023-04-18 15:48:42
我有點(diǎn)困惑為什么下面的代碼片段在第二次讀取同一個(gè)文件時(shí)返回正確的輸出:textCont = "Hello World"print("Original content of the file")print(open(filename).read())textFile = open(filename, "w")textFile.write(textCont)print("New file content:")textFile = open(filename)print(textFile.read())textFile.close()其中filename是包含一些現(xiàn)有數(shù)據(jù)的文件。該文件將被讀取、覆蓋,然后再次讀取。在上面的例子中,同一個(gè)變量被用于以寫入模式打開文件,然后以讀取模式打開文件。這工作正常并在第二次讀取時(shí)提供正確的輸出(顯示已覆蓋前一個(gè)的內(nèi)容)但是以下版本的代碼不起作用:textCont = "Hello World"print("Original content of the file")print(open(filename).read())textFile = open(filename, "w")textFile.write(textCont)print("New file content:")textFile_1 = open(filename)print(textFile_1.read())textFile.close()textFile_1.close()當(dāng)?shù)诙问褂糜糜谝詫懭肽J酱蜷_文件的變量以外的變量完成讀取時(shí),它返回一個(gè)空字符串。我知道當(dāng)?shù)诙巫x取同一個(gè)文件時(shí),它返回一個(gè)空字符串。但是為什么第一種情況下的代碼返回正確的輸出呢?誰能對(duì)此提供適當(dāng)?shù)慕忉專?
查看完整描述

2 回答

?
蕪湖不蕪

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊

首先要了解的是,當(dāng)您執(zhí)行 textFile.write 時(shí),不確定是否會(huì)立即寫入文件。這是為了通過將數(shù)據(jù)首先放入緩沖區(qū)來提高寫入效率,只有當(dāng)緩沖區(qū)已滿或調(diào)用刷新或文件關(guān)閉(內(nèi)部執(zhí)行刷新)時(shí),緩沖區(qū)才會(huì)寫入文件。

所以原因是不適用于兩個(gè)不同的變量名稱是因?yàn)閷懖僮鲝奈凑嬲l(fā)生在該行之前textFile_1.close()

它使用相同變量名“工作”的原因是,當(dāng)您重新綁定textFile = open(filename)以前綁定到的文件時(shí)textFile,現(xiàn)在沒有在任何地方引用它,因此垃圾收集器將其刪除。當(dāng)文件句柄被刪除時(shí),數(shù)據(jù)被寫入文件,因此您可以在之后讀取它。

另外,在處理文件時(shí)應(yīng)該使用 with open 習(xí)慣用法:

見下文,F(xiàn)ileDebug 包裝器向您顯示文件何時(shí)被刪除以及數(shù)據(jù)何時(shí)被寫入。

class FileDebug:

? ? def __init__(self, f, name):

? ? ? ? self.f = f

? ? ? ? self.name = name


? ? def close(self):

? ? ? ? print(f"Closing file (var: {self.name})")

? ? ? ? self.f.close()


? ? def write(self, *args, **kwargs):

? ? ? ? self.f.write(*args, **kwargs)


? ? def read(self, *args, **kwargs):

? ? ? ? return self.f.read(*args, **kwargs)


? ? def __del__(self):

? ? ? ? print(f"Del completed (var: {self.name})")


filename = "text.txt"

def different_name():

? ? textCont = "Hello World"

? ? print("Original content of the file")

? ? print(FileDebug(open(filename), "No name").read())

? ? textFile = FileDebug(open(filename, "w"), "textFile")

? ? textFile.write(textCont)

? ? print("New file content:")

? ? textFile_1 = FileDebug(open(filename), "textFile_1")

? ? print(textFile_1.read())

? ? textFile.close()

? ? textFile_1.close()


def same_name():

? ? textCont = "Hello World"

? ? print("Original content of the file")

? ? print(FileDebug(open(filename), "No name").read())

? ? textFile = FileDebug(open(filename, "w"), "textFile")

? ? textFile.write(textCont)

? ? print("New file content:")

? ? textFile = FileDebug(open(filename), "textFile")

? ? print(textFile.read())

? ? textFile.close()



different_name()

"""

Original content of the file

Del completed (var: No name)


New file content:


Closing file (var: textFile)

Closing file (var: textFile_1)

Del completed (var: textFile)

Del completed (var: textFile_1)

"""


#same_name()

"""

Original content of the file

Del completed (var: No name)


New file content:

Del completed (var: textFile) # the file is closed and the data is written

Hello World

Closing file (var: textFile)

Del completed (var: textFile)

"""


查看完整回答
反對(duì) 回復(fù) 2023-04-18
?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

第二個(gè)案例中的問題得到了解決。


解決方案是在寫入之后但再次讀取之前關(guān)閉文件。


print(open(filename).read())

textFile = open(filename, "w")

textFile.write(textCont)

textFile.close() // close the file from write mode before reading it again

print("your file content:")

textFile_1 = open(filename)

print(textFile_1.read())

textFile_1.close()


查看完整回答
反對(duì) 回復(fù) 2023-04-18
  • 2 回答
  • 0 關(guān)注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)