Search code examples
pythonreadline

How to print file.readline() line-number?


f = open("saves\\" + plrname + "_inv.txt", "r")
lines = f.readlines()
for x in range(500):
    if (x in lines) == "True\n":
        print(x)

There are 500 lines in the .txt file with either "True" or "False" I want the program to read them and print which ones are true by their line number. I've tried "for x in lines". I hope you can help me, thank you.

Example of how the txt file looks like:

True
True
True
False
False
True
False
False

Solution

  • you can use the built-in function enumerate:

    for index, x in enumerate(lines):
        if x == "True\n":
            print(index, x)