473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2224
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, a radio button) it will load into the form at the...
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 textbox and have it save back to the source...
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 since it has no default data presentation format....
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 another page that lets users modify the shared xml...
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 directory and passes the full file path to another...
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, on Windows XP. Every time I edit my .txt or .py...
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 stuff that was written in the input box will be saved...
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 in XML Notepad and save it, without making any...
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 have come across either add a new node to the doc...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.