In Python: I need to take a text file with a poem and create a copy but with numbered lines 1., 2., ect

1.1k Views Asked by At

I only just started I am just not sure how to place the numbers in front of each line with it being in a txt file. This is what I have so far.

def numbered(infile, outfile): for line in infile: line = #I am not sure where to go from here

def main():

try:
    infileName = input("Enter the file holding the poem:")
    infile = open(infileName,"r")

    outfileName = input("Enter the file name to hold the numbered poem:")
    outfile = open(outfileName,"w")
    print("Numbered version of" + infileName + "is here in" + outfileName +)
except IOError:
    print ("Error, could not find files")

main()

The end result should be Line one of poem line two of poem line three of poem ect

into: 1. Line one of poem 2. Line two of poem 3. Line three of poem ect

1

There are 1 best solutions below

0
On BEST ANSWER

I believe this can be helpful:

poem = open('first.txt', 'r')
output = open('second.txt', 'w')
count = 1
for line in poem.readlines():
    output.write(str(count) + " " + line + "\n")
    count += 1
poem.close()
output.close()