Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > edit text in a file and save it again

Reply
Thread Tools

edit text in a file and save it again

 
 
cyberco
Guest
Posts: n/a
 
      11-19-2006
I must be overlooking something here... I'm trying to edit a line in a
text file. I thought this was easy with fileinput, but all examples do
not write the line back to the file but simply 'print' it. I want to
open the file, edit the line and save it again. Is fileinput the right
module for that?

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      11-20-2006
At Sunday 19/11/2006 20:07, cyberco wrote:

>I must be overlooking something here... I'm trying to edit a line in a
>text file. I thought this was easy with fileinput, but all examples do
>not write the line back to the file but simply 'print' it. I want to
>open the file, edit the line and save it again. Is fileinput the right
>module for that?


Short answer: You can't.
Use an intermediate file. Keep reading from the original file and
writing to the intermediate file, until you find the line to replace.
Process that line and write to the output; then keep reading and
writing until the end.
Delete or rename the original file; and rename the intermediate file.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
 
Reply With Quote
 
 
 
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      11-20-2006
"cyberco" <(E-Mail Removed)> wrote:


> I must be overlooking something here... I'm trying to edit a line in a
> text file. I thought this was easy with fileinput, but all examples do
> not write the line back to the file but simply 'print' it. I want to
> open the file, edit the line and save it again. Is fileinput the right
> module for that?


Don't know about the fileinput module - haven't used it -
- but to change a line in a file, you normally have to
re write the whole thing - think of what has to happen if
your editing makes a line longer than what it was before
- how is it going to fit back into the existing,
smaller space?

And if the editing leaves the line shorter, what must be done
with the existing space that is "left over" and unused?

the way to change a line in a file is normally:

1) read and copy the lines before the line that must be changed to
a temporary file
2) read the line that needs to be changed, change it in memory,
and write it to the temp file.
3) read and copy the rest of the lines to the temp file.
4) close the files
5) delete the original
6) rename the temp to the original name.

hth - Hendrik


 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-20-2006
cyberco wrote:

> I must be overlooking something here... I'm trying to edit a line in a
> text file. I thought this was easy with fileinput, but all examples do
> not write the line back to the file but simply 'print' it. I want to
> open the file, edit the line and save it again. Is fileinput the right
> module for that?


http://docs.python.org/lib/module-fileinput.html

Optional in-place filtering: if the keyword argument inplace=1 is
passed to input() or to the FileInput constructor, the file is moved
to a backup file and standard output is directed to the input file (if
a file of the same name as the backup file already exists, it will be
replaced silently). This makes it possible to write a filter that
rewrites its input file in place.

</F>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python 2.4.4 Tkinter GUI Example for Modeless Dialog with Menu, Edit,List, Buttons, File Save etc. My Python Python 3 06-17-2010 06:56 AM
How can I read pdf file and save content in text file ? Alex Smith ASP .Net 0 06-15-2005 06:05 AM
Snapshot restraint - edit, edit, edit Alan Browne Digital Photography 24 05-10-2005 10:15 PM
Snapshot restraint - edit, edit, edit Patrick Digital Photography 0 05-06-2005 10:53 PM
Edit All Function for DataGrid, and Moving the Edit Function in a DataGrid Schultz ASP .Net 3 02-14-2005 04:47 AM



Advertisments