如何在Python中獲取當前開啟檔案的行號?


如果一個檔案在Python中開啟,並且您想找到開啟檔案的當前行號;本文將探討不同的方法,您可以使用某些模組、函式和方法來執行此任務。在此過程中,我們將討論一些帶有詳細解釋的程式碼示例,以闡明此任務中涉及的概念和策略。

使用 tell() 方法

您可以使用 open() 函式以讀取模式開啟檔案,並將其賦值給變數“file”。然後使用 read() 方法將檔案內容作為字串載入到另一個變數“contents”中。

要獲取當前行號,您可以使用 count() 方法計算此字串中換行符 ("\n") 的數量。並且您需要加 1 來從第 1 行開始計數。最後,使用 close() 函式關閉檔案,並將行號列印到控制檯以確認其正確性。這是一個實現示例

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

#myfile.txt
This is line number one.
This is line number two.
This is line number three.
This is line number four.

示例

#The file is opened in read mode
file = open("example.txt", "r")

#The contents of the file are read
contents = file.read()

# The current line number is fetched
line_number = contents.count("\n") + 1

# The file is closed
file.close()

# The current line number is printed
print("Current line number:", line_number)

輸出

如果執行上述程式碼,我們將得到以下結果。

Current line number: five

使用 enumerate() 函式

或者,您可以使用 Python 的內建 enumerate() 函式。enumerate() 方法將迭代檔案的每一行。它每次輸出一個元組,包含行號和行內容。

在這個例子中,我們不需要在迴圈內執行任何操作。我們只需要遍歷所有行以到達最後一行及其行號。

遍歷迴圈後,使用 close() 函式關閉檔案,並將當前行的行號列印到控制檯。

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

#myfile.txt
This is line number one.
This is line number two.
This is line number three.
This is line number four.

示例

# The file is opened in read mode
file = open("myfile.txt", "r")

# Iteration is done over the lines and the current line number is fetched
for line_number, line in enumerate(file, start=1):
   pass

# The file is closed
file.close()

# The current line number is printed
print("Current line number:", line_number)

輸出

如果執行上述程式碼,我們將得到以下結果。

Current line number: 5

使用 linecache 模組

示例

這段程式碼匯入了 linecache 模組,該模組方便了從檔案中讀取行。

這段程式碼開啟檔案,計算檔案的總行數,然後使用 total_lines 作為行號來獲取檔案中的最後一行。換行符的數量加 1 以獲得當前行號。

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

#myfile.txt
This is line number one.
This is line number two.
This is line number three.
This is line number four.

示例

import linecache

file_path = "myfile.txt"
import linecache

file_path = "myfile.txt"

# Get the total number of lines in the file
total_lines = len(open(file_path).readlines())

# The current line number is fetched
line_number = total_lines

# The current line number is printed
print("Current line number:", line_number)

輸出

如果執行上述程式碼,我們將得到以下結果。

Current line number: 5

使用 readline() 方法

示例

  • 當使用 open() 函式時,程式碼以讀取模式開啟檔案 "myfile.txt" 並將其賦值給變數“file”。

  • 在 while 迴圈內,readline() 方法用於逐行讀取檔案,直到到達檔案末尾。

  • 在每次迭代中,line_number 增加 1。

  • 迴圈終止後,使用 close() 方法關閉檔案,並將當前行號列印到控制檯。

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

#myfile.txt
This is line number one.
This is line number two.
This is line number three.
This is line number four.

示例

# The file is opened in read mode
file = open("myfile.txt", "r")

# The file is read line by line till the end
line_number = 0
while file.readline():
   line_number += 1

# The file is closed
file.close()

# The current line number is printed
print("Current line number:", line_number)

輸出

如果執行上述程式碼,我們將得到以下結果。

Current line number: 5

使用 seek() 方法

示例

  • 檔案 "myfile.txt" 以讀取模式開啟並賦值給變數“file”。

  • seek() 方法將檔案位置移動到檔案末尾。引數 0 表示從當前位置的偏移量,2 表示檔案末尾的參考點。

  • tell() 方法用於獲取檔案中的當前位元組偏移量或檔案位置,並將其賦值給 byte_offset 變數。

  • read() 方法用於讀取檔案內容,然後將其儲存在 contents 變數中。

  • 使用 count() 方法計算檔案內容中的換行符 ("\n") 數量,並加 1 以考慮結尾行。

  • 最後,使用 close() 方法關閉檔案,並將當前行號列印到控制檯。

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

#myfile.txt
This is line number one.
This is line number two.
This is line number three.
This is line number four.

示例

with open("myfile.txt", "r") as file:
    # The end of the file is reached
    file.seek(0, 2)

    # The file position or current byte offset is fetched
    byte_offset = file.tell()

    # Move the file pointer to the beginning
    file.seek(0)

    # Read the file contents
    contents = file.read()

    # The number of newline characters is counted
    line_number = contents.count("\n") + 1

# The current line number is printed
print("Current line number:", line_number)

輸出

如果執行上述程式碼,我們將得到以下結果。

Current line number: 5

簡而言之,我們已經看到了在 Python 中獲取開啟檔案的當前行號的各種程式碼示例。其他示例展示了使用各種不同方法來實現相同過程的各種方法,即在 Python 中獲取開啟檔案的當前行號。您可以始終選擇最符合您需求的方法,並使用它來獲取系統上開啟檔案的當前行號。

更新於:2023年7月20日

3K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.