如何在 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() 方法在檔案物件上呼叫,以將寫入位置設定為檔案中指定的 location。傳遞給 seek() 的引數為 0(偏移量)和 2(whence)。偏移量引數 0 表示寫入位置將相對於檔案開頭設定。whence 引數 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:您首先以所需模式(r 表示讀取、w 表示寫入或 a 表示追加)開啟檔案,並使用 open() 函式。

  • 步驟 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+ 閱讀量

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.