返回: Python基礎 索引頁
下面的例子,讀取一個名為 test.txt 的文件內容,並逐行打印。
test.txt 位於與我的python 程序同一個目錄下,其內容為:
This is line 1 This is line 2
我的程序如下:
myfile = open('test.txt') line = myfile.readline() while line: current_line = line.strip() print (current_line) line = myfile.readline() myfile.close()
輸出結果如下:
This is line 1 This is line 2
返回: Python基礎 索引頁