472,958 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Can I change one line in a file without rewriting the whole thing?

In Perl, there is a module called "Tie::File". What it does is tie a
list to each line of a file. Change the list, and the file is
automatically changed, and on top of this, only the bits of the file
that need to be changed are written to disk. At least, that's the
general idea.

I was wondering if something roughly similar could be done in Python,
or at the very least, if I can avoid doing what amounts to reading the
whole file into memory, changing the copy in memory, and writing it
all out again.

Jul 14 '07 #1
1 3056
En Fri, 13 Jul 2007 23:46:24 -0300, J. J. Ramsey <jj******@pobox.com>
escribió:
In Perl, there is a module called "Tie::File". What it does is tie a
list to each line of a file. Change the list, and the file is
automatically changed, and on top of this, only the bits of the file
that need to be changed are written to disk. At least, that's the
general idea.
That usually means, rewriting from the first modified line to the end of
the file.
I was wondering if something roughly similar could be done in Python,
or at the very least, if I can avoid doing what amounts to reading the
whole file into memory, changing the copy in memory, and writing it
all out again.
Simplest aproach:

lines = list(open("myfile.txt"))
del lines[13]
lines[42] = "Look ma! Replacing line 42!\n"
open("myfile.txt","w").writelines(lines)

This of course reads the whole file in memory, but it's a compact way if
you require random line access.
If you can serialize the file operations, try using the fileinput module
with inplace=1.

(Having a true Tie::File implementation for Python would be a nice
addition to the available tools...)

--
Gabriel Genellina

Jul 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: balgach | last post by:
Greetings all, I have a group of rather large files (by group i mean close to 2x10^7 files, each 12-15megs) now i need information which is stored in just the last 512 bytes of each file. i...
3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
4
by: nan | last post by:
Hi all. Anyone has an idea of how to open a file without know the complete name of it, without opening the directory (with opendir) and test each file? For example, I have this: ...
4
by: doritrieur | last post by:
How do i read line from an input file, without the /n ? the readline function returns also the /n character at the end. i need to read a line without the training /n is this possible? thanks,...
15
by: MCondon77 | last post by:
This is probably a common problem, but as a novice programmer I'm trying to figure out how to write 0x0A to a BIN file without having it automatically write 0x0D. Here's an example of the code:...
0
by: ashish arora | last post by:
Hi I have some dll files. now i have to change some existing functionality of asp pages, but i dont have source code of asp, how shall i change the dll files. thanks in advance...
18
by: John Bailo | last post by:
I want to write an Excel file (.xls format) from some database data. I don't want to use Excel.exe because of all the automation and security issues. Does Microsoft document the .xls file...
5
by: ganesh.kundapur | last post by:
Hi, I want to know how to compile the C source file without using the compiler directly. I mean to say by using compiler componets such as ( cpp, cc, as, ld ). I tried to compile the fallowing...
2
nicebasic
by: nicebasic | last post by:
I have written a small VB Application to play my Audio Files the way I like. But, up to now, I haven't been able to change the speed of the audio file without changing its pitch. As you know, it's...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.