I need to write the line numbers to an already existing text file in python 3. They have asked for the first 5 columns of the text file to contain a 4 digit line number followed by a space.
I've tried to do this by creating a for loop that reads the lines and adds a number (1) to the beginning of the line and then increments the number for the next line but is hasn't been working. It keeps coming up with an error saying that I cant join a string and an integer in my output.
What would the simplest way to do this be?
numberedfile = open("test.txt", "r")
numberedlist = numberedfile.readline()
for line in numberedlist:
x = 1
nrline = (x + line)
x += 1
print(nrline)
numberedlist = numberedfile.readline()
numberedfile.close()