Inserting text into files

This is the place for queries that don't fit in any of the other categories.

Inserting text into files

Postby sahasrabuddhe » Fri Mar 22, 2013 6:40 am

I am a embedded fresher, working in a company just now. I need to insert a line in a file which is already exist. Line no may very. But the text just before it is constant. Can anybody tell me, how to do this ?
sahasrabuddhe
 
Posts: 1
Joined: Fri Mar 22, 2013 5:06 am

Re: Files

Postby Yoriz » Fri Mar 22, 2013 6:51 pm

sahasrabuddhe wrote:I am a embedded fresher, working in a company just now. I need to insert a line in a file which is already exist. Line no may very. But the text just before it is constant. Can anybody tell me, how to do this ?

  1. Open the file your reading and a file to write too.
  2. Step through the lines reading from the original and writing to the new file.
  3. when you find the before line add the new line into the new file.
  4. Continue looping through copying the rest.
  5. If you only want the one version of the file, delete the old file and rename the new file to the old file name.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 7 guests