[Tutor] Replacing the string in a file
Andreas Kostyrka
andreas at kostyrka.org
Fri Jan 22 12:53:00 CET 2010
The modes for updating files are + suffixed.
r+ Open for reading and writing. The stream is positioned at the
beginning of the file.
w+ Open for reading and writing. The file is created if it does
not exist, otherwise it is trun‐
cated. The stream is positioned at the beginning of the file.
a+ Open for reading and appending (writing at end of file). The
file is created if it does not
exist. The initial file position for reading is at the
beginning of the file, but output is
always appended to the end of the file.
The bigger issue here is, that depending upon your filesystem it might be
faster to just write a new file with the changed content.
If you want to replace it inplace, I'd consider something like that:
import mmap
f = open("/tmp/test.txt", "r+")
m = mmap.mmap(f.fileno(), 0)
idx = 0
count = 0
while True:
idx = m.find("file", idx)
if idx >= 0:
m[idx:idx + 4] = "FILE"
count += 1
idx += 1
else:
print "%d file => FILE replaces" % count
break
m.close()
f.close()
andreas at andidesk:/tmp$ python ./test.py
39 file => FILE replaces
You've got basically 3 strategies:
1) read the file completely into memory via read => uses memory.
2) read the file block-wise => complicated because you need to recognize
matches that are split between blocks.
3) memory map the file => uses address space, but the OS does know that you
are using that "memory" to access the file.
1 is the shortest code, most resource usage. 2 is complicated code, low
resource usage, 3 is slightly longer (because mmap objects have no .replace
method), and comparable resource usage to 2.
Andreas
Andreas
Am Freitag, 22. Januar 2010 12:23:53 schrieb Dave Angel:
> vanam wrote:
> > Thanks for your mail.
> >
> > As you have suggested i have changed the mode to 'rw' but it is
> > throwing up an error as below
> >
> > *******************
> > IOError: [Errno 22] invalid mode ('rw') or filename: 'data.txt'
> > *******************
> > I am using python 2.6.4.
> >
> > But Script is managed to pass with 'a+' mode/r+ mode.
> >
> > log = open('data.txt','r+/a+')
> > for x in log:
> > x = x.replace('Python','PYTHON')
> > print x,
> > log.close()
> >
> > It had properly written and replaced Python to PYTHON.
> >
> > Thanks for your suggestion.
> >
> >
> > <snip>
>
> That won't work. Better test it some more. Without some form of
> write() call, you're not changing the file.
>
>
> There are several workable suggestions in this thread, and I think
> fileinput is the easiest one.
>
> DaveA
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list