如何查看python中的文件中是否存在行?

时间:2013-11-28 11:02:46

标签: python file

我正在创建一个高分数据库,我需要检查文件中的一行是否存在于我创建的文件中。

我是否会使用for循环查看python中的文件中是否存在字符串。

由于

1 个答案:

答案 0 :(得分: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.")