如何在 Python 中設定檔案讀取和寫入位置?


作為檔案處理操作的一部分,在 Python 中,您可以執行設定檔案內讀取和寫入位置的操作,以訪問或修改其內容的特定部分。此功能允許您瀏覽並探索檔案,並在所需位置執行各種操作。透過本文,我們將學習掌握使用 Python 設定檔案讀取和寫入位置的操作。您可以隨時參考以下提供的描述性和詳細說明以及程式碼示例,以瞭解並掌握在檔案中定位的技能。

設定檔案的讀取位置

為了能夠設定檔案內的讀取位置,您可以使用 seek() 方法。以下是它的工作原理

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

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

  • 步驟 3:您開始使用 seek() 方法,並將所需位置作為引數來設定讀取位置。

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

讓我們參考一個簡單的程式碼片段來解釋這個過程

示例

假設我們有一個如下所示的文字檔案

#myfile.txt
This is a test file

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

# Set the read position to the desired location (e.g., 10th character)
file.seek(10)

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

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

# Close the file
file.close()

當以上程式碼執行時,開啟 myfile.txt 檔案後顯示以下內容。

輸出

Content: e test file

設定檔案的寫入位置

讓我們學習如何在檔案中設定“寫入”位置;以下是您可以遵循的步驟

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

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

  • 步驟 3:接下來,您使用 seek() 方法,並將所需位置作為引數來設定“寫入”位置。

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

讓我們看一個示例程式碼片段

示例

假設我們有一個如下所示的文字檔案

#myfile.txt
This is a test file

這裡,seek() 方法被呼叫在檔案物件上,以將寫入位置設定為檔案中指定的位置。傳遞給 seek() 的引數為 0(偏移量)和 2(起始位置)。偏移量引數 0 表示寫入位置將相對於檔案開頭設定。起始位置引數 2 表示寫入位置將設定為檔案末尾。

因此,file.seek(0, 2) 將寫入位置設定為檔案末尾。

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

# Set the write position to the desired location (e.g., at the end of the file)
file.seek(0, 2)

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

當以上程式碼執行時,開啟 myfile.txt 檔案後顯示以下內容

輸出

This is new content.

組合讀取和寫入位置

您也可以在檔案中組合讀取和寫入位置。讓我們看一個演示此功能的示例

示例

假設我們有一個如下所示的文字檔案

#myfile.txt
This is a test file

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

# Set the read position to the desired location (e.g., 5th character)
file.seek(5)

# Read the content from the new position
content = file.read()
print("Content:", content)

# Set the write position to a new location (e.g., 15th character)
file.seek(15)
# Write new content at the write position
file.write("New content")
# Close the file
file.close()

當以上程式碼執行時,開啟 myfile.txt 檔案後顯示以下內容。

輸出

Content: is a test file

使用 tell() 設定讀取和寫入位置

還有一種方法可以設定檔案內的讀取和寫入位置;例如,使用 tell() 方法確定當前位置,然後使用 seek() 方法移動到所需位置。讓我們學習如何操作

  • 步驟 1:您首先使用 open() 函式以所需的模式(r 表示讀取、w 表示寫入或 a 表示追加)開啟檔案。

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

  • 步驟 3:接下來,您使用 tell() 方法獲取檔案內的當前位置,並將其儲存在一個變數中。

  • 步驟 4:然後,您使用 seek() 方法,並將所需位置作為引數來設定讀取或寫入位置。

  • 步驟 5:最後,您在新位置執行所需的操作。

讓我們看一個示例程式碼片段

示例

假設我們有一個如下所示的文字檔案

#myfile.txt
This is a test file

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

# Get the current read position
position = file.tell()

# Set the read position to the desired location (e.g., 12th character)
file.seek(12)

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

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

# Close the file
file.close()

當以上程式碼執行時,開啟 myfile.txt 檔案後顯示以下內容。

輸出

Content: st file

使用偏移量設定讀取和寫入位置

必須知道,您還可以透過向 seek() 方法提供偏移量值來設定讀取和寫入位置。此偏移量指向從當前位置向前或向後移動的位元組數。讓我們看一個示例

示例

這裡,我們使用 seek() 方法,偏移量為 5,值為 1,表示從當前位置向前移動。這允許我們將寫入位置設定為當前位置前 5 個位元組,並在該位置寫入新內容。

假設我們有一個如下所示的文字檔案

#myfile.txt
This is a test file

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

# Set the write position to the desired location (e.g., move 5 bytes forward from the current position)
file.seek(5, 0)

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

# Close the file
file.close()

當以上程式碼執行時,開啟 myfile.txt 檔案後顯示以下內容。

輸出

New content

設定檔案內讀取和寫入位置的檔案處理操作或任務使您可以訪問內容的特定部分並根據需要對其進行修改。在本文中,我們介紹並討論了一些示例,這些示例說明了如何使用 seek() 方法設定檔案內的讀取和寫入位置。我們還介紹了一些其他示例,這些示例演示瞭如何使用 tell() 方法並向 seek() 方法提供偏移量,以便在 Python 中設定檔案內的讀取和寫入位置。透過認真學習並練習相同的易於理解的說明和程式碼片段,您可以對 Python 中的檔案定位有一個全面而紮實的瞭解。必須確保在執行完操作後關閉檔案,以確保正確處理系統資源。

更新於: 2023-07-13

3K+ 閱讀量

啟動您的 職業生涯

透過完成課程獲得認證

立即開始
廣告