可以通過使用open()函數打開文件并使用readline()方法逐行讀取文件內容。以下是一個示例:
open()
readline()
file_path = "example.txt" with open(file_path, 'r') as file: for line in file: print(line.strip())
在上面的示例中,example.txt文件將被打開并按行讀取。然后,使用strip()方法去除每行末尾的空格和換行符,并將每行內容打印出來。
example.txt
strip()