473,748 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3097
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("myfi le.txt"))
del lines[13]
lines[42] = "Look ma! Replacing line 42!\n"
open("myfile.tx t","w").writeli nes(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
1639
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 was wondering if there is a way to strip out this information without loading the entire file into memory. Right now im doing it with fopen() and fread() and it takes several hours to process all the files. obviously i know exactly where the...
3
23956
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
8327
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: 1070471736268 and the complete file name is:
4
2823
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, Dorit
15
6319
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: tmp_ifp = fopen("tmp_name.bin", "w"); fprintf(tmp_ifp, "%c", 0x0A); When I open the tmp_name.bin file with a hex editor you notice that both 0x0D and 0x0A are written. I tried using fwrite, but the same thing occurs. Any ideas?
0
4220
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
25728
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 format anywhere? Is there any c# class that will let me do this?
5
3345
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 code as fallows file name: hello.c # include <stdio.h> int main ( void )
2
3289
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 easy the change the speed of the Media/Audio file being played with MediaPlayer.Rate using Windows Classic Media Player whose OCX file has this name: "msdxm.ocx". The value for Rate ranges from 75 to 150. But, when you change the rate of audio...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.