如何在 Python 中只讀開啟檔案?


在 Python 中,檔案處理涉及許多操作;其中一項任務是開啟檔案以進行讀取,這使您可以訪問並從檔案中檢索資料。在本文中,我們將討論並學習如何在 Python 中以讀取模式開啟檔案;我們將使用一些帶有自解釋描述的程式碼示例來幫助您理解和掌握在 Python 中讀取檔案的過程。

開啟檔案以讀取並列印其內容

要開啟檔案以讀取並列印其內容,您可以按照以下步驟操作

  • 步驟 1:您可以使用 open() 函式開啟檔案。然後,您將檔案路徑作為第一個引數提供,並使用模式“r”指示讀取模式。

  • 步驟 2:然後,您將返回的檔案物件分配給一個變數,以便進行進一步的操作,例如讀取檔案內容。

  • 步驟 3:您還可以使用 read() 方法讀取檔案的內容並將其儲存在一個變數中。

  • 步驟 4:然後,您可以使用 print() 函式列印檔案的內容或對內容執行任何必要的操作。

  • 步驟 5:最後,您可以關閉檔案以確保正確處理系統資源。

示例

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

#myfile.txt
This is a test file

# Open the file for reading

file = open('myfile.txt', 'r')

# Read the content of the file
content = file.read()

# Print the content
print(content)

# Close the file
file.close()

當我們執行上述程式碼時,當 myfile.txt 被開啟並讀取現有內容時,我們將獲得以下輸出。

輸出

#myfile.txt
This is a test file

逐行開啟檔案以讀取

為了逐行讀取檔案,請按照以下步驟操作

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

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

  • 步驟 3:您可以選擇使用迴圈遍歷檔案中的每一行。

  • 步驟 4:可以處理迴圈中的每一行(例如,列印它或執行其他操作)。

  • 步驟 5:最後,您可以關閉檔案以釋放系統資源。

示例

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

#myfile.txt
This is a test file.
This is the first line.
This is the second line.
This is the third line.

# Open the file for reading
file = open('myfile.txt', 'r')

# Read the file line by line
for line in file:
    # Process each line (e.g., print it)
    print(line)

# Close the file
file.close()

當我們執行上述程式碼時,當 myfile.txt 被開啟並讀取現有內容時,我們將獲得以下輸出。

輸出

#myfile.txt

This is a test file.
This is the first line.
This is the second line.
This is the third line.

使用上下文管理器讀取檔案

您必須知道,Python 中的上下文管理器是一個物件,它描述了當使用“with”表示式執行程式碼塊時將建立的執行時上下文。它提供了一種在恰當的時間分配和釋放資源的方法。例如,“with”語句可用於在 Python 中開啟檔案並在完成使用後關閉它。

透過這種方式,Python 使得使用上下文管理器讀取檔案變得容易。正如您現在所瞭解的,上下文管理器有助於自動關閉檔案,即使發生異常或錯誤也是如此。以下是如何使用上下文管理器讀取檔案

  • 步驟 1:您可以使用“with”語句和 open() 函式以讀取模式開啟檔案。

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

  • 步驟 3:最後,您在 with 塊中處理檔案內容(例如,列印它或執行其他操作)。

示例

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

#myfile.txt
This is a test file

# Open the file for reading using a context manager
with open('myfile.txt', 'r') as file:
    # Process the file content (e.g., print it)
    content = file.read()
    print(content)

當我們執行上述程式碼時,當 myfile.txt 被開啟並讀取現有內容時,我們將獲得以下輸出。

輸出

#myfile.txt
This is a test file

讀取檔案並計算行數

為了能夠讀取檔案並計算其包含的行數,您可以按照以下步驟操作

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

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

  • 步驟 3:然後,您繼續初始化一個計數器變數以跟蹤行數。

  • 步驟 4:您可以使用迴圈遍歷檔案中的每一行。

  • 步驟 5:您可以繼續為每一行將計數器變數加 1。

  • 步驟 6:最後,您可以關閉檔案以釋放系統資源。

示例

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

#myfile.txt
This is a test file.
This is the first line.
This is the second line.

# Open the file for reading
file = open('myfile.txt', 'r')

# Initialize the counter variable
line_count = 0

# Read the file line by line and count the lines
for line in file:
    line_count += 1

# Print the line count
print("Number of lines:", line_count)

# Close the file
file.close()

當我們執行上述程式碼時,當 myfile.txt 被開啟並讀取現有內容以及計算行數時,我們將獲得以下輸出。

輸出

Number of lines: 4

讀取檔案並提取特定資訊

為了能夠讀取檔案並從中提取特定資訊,您可以使用以下步驟

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

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

  • 步驟 3:然後,您使用 readlines() 方法將檔案內容讀取到一個變數中,該方法返回一個行列表。

  • 步驟 4:接下來,您處理檔案的內容以提取所需的資訊。您可以根據您的特定需求使用字串操作技術、正則表示式或其他方法。

  • 步驟 5:最後,您可以關閉檔案以確保正確處理系統資源。

示例

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

#myfile.txt
This is a test file.
This is the first line.
This is the second line.

# Open the file for reading

file = open('myfile.txt', 'r')

# Read the file content into a list of lines
lines = file.readlines()

# Extract specific information from the file content
for line in lines:

    if 'test' in line:
        print(line)
# Close the file
file.close()

當我們執行上述程式碼時,當 myfile.txt 被開啟並讀取現有內容以及提取包含單詞“test”的行並列印為以下輸出時,我們將獲得以下輸出。

輸出

This is a test file.

以讀取模式開啟檔案是 Python 檔案處理領域的基本或基礎操作。在本文中,我們已經看到並討論了一些示例,這些示例演示瞭如何以讀取模式開啟檔案並檢索其內容。我們還提供了其他程式碼示例,這些示例演示了讀取檔案並計算行數,以及讀取檔案並提取特定資訊。透過遵循和應用系統化的解釋和程式碼示例,您可以深入瞭解 Python 中讀取檔案的概念。需要注意的是,在讀取後務必關閉檔案,以確保正確處理系統資源。

更新於:2023年7月13日

764 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告