Python Forum
reading text file and writing to an output file precedded by line numbers
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading text file and writing to an output file precedded by line numbers
#1
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()
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Would you be able to give me an example of the best way to fix that line?
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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
Reply
#6
'/* %d */ %s' % total, line
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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
Reply
#8
Sorry, it's been a while since I used that syntax (it's a bit out dated). It should be '/* %d */ %s' % (total, line)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation URGENT: How to plot data from text file. Trying to recreate plots from MATLAB JamieAl 4 3,534 Dec-03-2023, 06:56 AM
Last Post: Pedroski55
  dictionary output to text file (beginner) Delg_Dankil 2 1,163 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Output File ? Kessie1971 11 1,963 May-11-2023, 08:31 AM
Last Post: buran
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,407 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Help with university work - reading and writing files MrKnd94 3 1,189 Nov-01-2022, 08:29 PM
Last Post: deanhystad
  Excel File reading vanjoe198 1 2,026 Mar-31-2021, 11:53 AM
Last Post: snippsat
  Reading a text until matched string and print it as a single line cananb 1 2,017 Nov-29-2020, 01:38 PM
Last Post: DPaul
  Convert list of numbers to string of numbers kam_uk 5 2,990 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,548 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  Delete line of file dionatandiego11 2 1,898 Aug-05-2020, 05:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020