如何在 Python 中使用 seek() 方法重置檔案讀寫位置?


在 Python 檔案處理中,有幾個函式和方法具有不同的功能,用於實現特定的目的和任務。在上述這類方法中,seek() 方法使您能夠重置檔案中的讀寫位置。當您需要將游標移動到檔案中的特定位置以執行後續的讀或寫操作時,此方法的功能非常有用。在本文中,我們將研究幾種如何使用 seek() 方法在 Python 中重置讀寫位置的方法。您可以參考以下易於理解的解釋和程式碼示例來掌握重置檔案中讀寫位置的概念和實踐。

將讀位置重置到檔案開頭

要能夠將讀位置重置到檔案開頭,請按照以下步驟操作

  • 步驟 1:您首先使用 open() 函式以讀取模式開啟檔案。

  • 步驟 2:然後繼續將返回的檔案物件分配給一個變數。

  • 步驟 3:接下來,使用 seek() 方法並將偏移量 0 作為引數,將讀位置重置到檔案開頭。

  • 步驟 4:最後,您可以使用 read() 方法從新位置讀取內容。

示例

假設有一個名為 myfile.txt 的檔案,其內容如下所示

#myfile.txt
This is a test file

# Open the file in read mode
file = open('myfile.txt', 'r')

# Reset the read position to the beginning of the file
file.seek(0)

# Read the content from the new position
content = file.read()

# Print the content
print("Content:", content)

# Close the file
file.close()

當執行上述程式碼並開啟檔案 myfile.txt 時,我們將得到以下結果

輸出

This is a test file

將寫位置重置到檔案開頭

按照以下步驟,您將能夠將寫位置重置到檔案開頭。

  • 步驟 1:您首先使用 open() 函式以寫入模式開啟檔案。

  • 步驟 2:然後繼續將返回的檔案物件分配給一個變數。

  • 步驟 3:接下來,使用 seek() 方法並將偏移量 0 作為引數,將寫位置重置到檔案開頭。

  • 步驟 4:最後,使用 write() 方法寫入所需內容。

示例

假設有一個名為 myfile.txt 的檔案,其內容如下所示

#myfile.txt
This is a test file

# Open the file in write mode
file = open('myfile.txt', 'w')

# Reset the write position to the beginning of the file
file.seek(0)

# Write new content at the write position
file.write("This is new content.")

# Close the file
file.close()

當執行上述程式碼並開啟檔案 myfile.txt 時,我們將得到以下結果

輸出

This is new content.

將讀/寫位置重置到特定位置

還有其他方法可以達到相同的目的;您還可以透過向 seek() 方法提供所需的偏移量值,將讀/寫位置重置到檔案中的特定位置。讓我們來看一個例子

示例

假設有一個名為 myfile.txt 的檔案,其內容如下所示

#myfile.txt
This is a test file

# Open the file in read and write mode
file = open('myfile.txt', 'r+')

# Reset the read/write position to a specific location (e.g., 15th character)
file.seek(15)

# Perform read or write operations at the new position
content = file.read()
print("Content:", content)

# Close the file
file.close()

當執行上述程式碼並開啟檔案 myfile.txt 時,我們將得到以下結果

輸出

Content: file.

相對於當前位置重置讀位置

  • 步驟 1:您首先使用 open() 函式以讀取模式開啟檔案。

  • 步驟 2:然後繼續將返回的檔案物件分配給一個變數。

  • 步驟 3:接下來,使用 seek() 方法並使用正或負偏移量值,將讀位置從當前位置向前或向後移動。

  • 步驟 4:最後,您可以使用 read() 方法從新位置讀取內容。

示例

假設有一個名為 myfile.txt 的檔案,其內容如下所示

#myfile.txt
This is a test file

# Open the file in read mode
file = open('myfile.txt', 'r')

# Move the read position 5 characters forward from the start position
file.seek(5, 0)

# Read the content from the new position
content = file.read()

# Print the content
print("Content:", content)

# Close the file
file.close()

輸出

Content: is a test file.

相對於當前位置重置寫位置

最好按照以下步驟重置檔案內相對於當前位置的寫位置。

示例

在這裡,在這個例子中,我們首先以讀取模式開啟檔案,並將整個內容讀取到 content 變數中。然後我們關閉檔案。我們透過在所需位置(在本例中,從開頭 10 個位元組)插入所需的新內容來修改檔案的內容。最後,我們以寫入模式開啟檔案,將修改後的內容寫回檔案,並關閉它。

假設有一個名為 myfile.txt 的檔案,其內容如下所示

#myfile.txt
This is a test file

# Open the file in read mode
file = open('myfile.txt', 'r')
content = file.read()
file.close()

# Modify the content
new_content = content[:10] + "New content" + content[10:]

# Open the file in write mode
file = open('myfile.txt', 'w')

file.write(new_content)
file.close()

當執行上述程式碼並開啟檔案 myfile.txt 時,我們將得到以下結果

輸出

This is a New contenttest file.

在 Python 檔案處理中,當您希望稍後導航到某個位置進行讀或寫操作時,重置檔案內讀寫位置的過程非常重要。在本文中,我們提供了一些示例,展示瞭如何使用 seek() 方法在 Python 中重置讀寫位置。我們還提供了其他示例,清楚地說明了如何使用 seek() 方法和相對偏移量值來重置讀寫位置。透過遵循清晰易懂的解釋和程式碼片段,您一定能夠擴充套件您對重置檔案內讀寫位置技能的理解。最好不要忘記在執行完操作後關閉檔案,以確保正確處理系統資源。

更新於:2023-07-13

11K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告