2013-11-28 34 views
-3

我正在創建高分數據庫,並且需要檢查文件中是否存在我創建的文件中的一行。如何查看python文件中是否存在行?

我會用for循環來查看python文件中是否存在字符串。

感謝

+1

你嘗試過這麼遠嗎?那麼當你這樣做的時候,如果有錯誤發生在這裏 –

回答

2

如果你只想找一個是或否的答案,該行是靜態的,你可以簡單地做

search = "This is the line I'm looking for.\n" 
with open("highscores.txt") as file: 
    if any(line == search for line in file): 
     print("Found it!") 
    else: 
     print("Move along.") 
相關問題