Search code examples
pythontext-filesreadline

reading each line of a text file preceded by line numbers


demoFile=open("lambpoem.txt","r")
for i in demoFile:
    print(i)

how do I modify the code in order for it to include the line number before the text in that line?


Solution

  • The way that I do this but this is certainly not the only way would to do so.

    with open('File.txt', 'r') as f:
        files = f.readlines()
    for i in range(len(files)):
        print(i, files[i])