By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

edit text in a file and save it again

P: n/a
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?

Nov 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 20 '06 #2

P: n/a
"cyberco" <cy*****@gmail.comwrote:

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
Nov 20 '06 #3

P: n/a
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>

Nov 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.