在 Python 中 Selenium 的 readline() 和 readlines() 有什麼不同?
以下是 readline() 和 readlines() 方法之間的區別。
readlines()
該方法將一次讀取檔案的所有內容。
該方法讀取檔案的所有內容並將其儲存在列表中。
該方法使用 readline() 讀取到行尾並返回一個列表。
readline()
該方法將讀取檔案中的一個行。
如果檔案沒有以新行結尾,新行字元將留在字串末尾,最後一行的字元將被忽略。
該方法使用 readline() 讀取到行尾並返回一個列表。
示例
使用 readline() 的程式碼實現
#open the file for read operation fl = open('pythonfile.txt') # reads line by line ln = fl.readline() while ln!= "": print(ln) ln = fl.readline() #close the file fl.close()
使用 readlines() 的程式碼實現
#open the file for read operation fl = open('pythonfile.txt') # reads line by line and stores them in list for ln in fl.readlines(): print(ln) #close the file fl.close()
廣告