472,328 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

edit text in a file and save it again

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
3 2165
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: matt | last post by:
I am using a text file as a database, each field delimited by || I want to be able to print all lines to a page, then by selecting one (with, say,...
4
by: Option^Explicit | last post by:
What I'm trying to do: Open a text file and display the contents in a text box (I've done this) Need to be able to edit the file from within the...
0
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you...
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have...
4
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a...
3
by: Mark | last post by:
Hello, What I need to know is if there is a better method to run/edit modules on my pc. I'm currently running the IDLE shell under Python 2.5,...
11
by: Lamer | last post by:
I was wondering how to save PHP variables to a txt file and then retrieve them again. Example: There is an input box, after submitted the...
2
by: Derek Hart | last post by:
If I save an xml file (with the schema) from a dataset, using Dataset.WriteXML, I then wish to edit the file using XML Notepad. If I just open it...
4
by: gator | last post by:
Hello, I am trying to edit a specific node in an XML file using C#. I have searched around for example code to do this. The only solutions I...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.