Python Forum

Full Version: reading text file and writing to an output file precedded by line numbers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In Python, Write a program that reads a file containing a text (input.txt). Read each line and send it to the output file (output.txt), preceded by line numbers. If the input file is:5 pts
Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go!

Then the program produces the output file
/* 1 */ Mary had a little lamb
/* 2 */ Whose fleece was white as snow.
/* 3 */ And everywhere that Mary went,
/* 4 */ The lamb was sure to go!

prompt the user for the input and output file names.

So this is what ive written so far but it has a file not found error no matter wether I put the full location or file name, any ideas would help and double check to make sure I got the rest proper please and thank you!

#prompts user for input/output file name
inputFileName = input('Input file name')
outputFileName = input('Output file name')

#opens the input out files
inputFile = open(inputFileName, "r")
outputFile = open(outputFileName, "w")

total = 0
#to copy and add line numbers and close the files
for line in inputFile:
    string = inputFile.readlines()
    total = total +1
    outputFile.write("/*",total ,string, "*/")



#closes the files
inputFile.close()
outputFile.close()
I don't get a file not found error, as long as I use a file name in the same folder that I'm running it in.

Your write on line 14 is incorrect. First of all, you can't use multiple arguments in write like you can in print. You need to convert everything to one string before writing. Second of all, you want to be outputting line, not string. Since you use readlines with an s to get string, it's every line in the file. The loop variable line is already the line of the file associated with total.
Would you be able to give me an example of the best way to fix that line?
Well, this being for a class, I would use something you've learned, so it doesn't look like you got the answer off the internet. Have they taught you string concatenation?

text = 'str' + 'ing' + ' con' + 'cat' + 'en' + 'a' + 'tion'
I prefer the format method of strings. You can see the full format method syntax here.

Edit: Note the first one will require you to convert total to a string with str(), but the second one handles that for you.
the book is python for everyone 2e. it covers the string format operator ex
"%10.2f" % price 
but it does not cover the .format style
'/* %d */ %s' % total, line
ok so I tried it to ways and both came back saying not enough arguments for format strings,


1st I tried
outputFile.write('/* %d */ %s' % total, line)


#and then I tried 


line1 = ('/* %d */ %s' % total, line)
    outputFile.write (line1)
please tell me how im being an idiot now lol
Sorry, it's been a while since I used that syntax (it's a bit out dated). It should be '/* %d */ %s' % (total, line)