modify files?

Ingo 'resISt' Siebert res1st at gmx.de
Fri Feb 9 23:17:03 CET 2001


Hi,
i´m very new to Python but i have to learn that. So i´m starting with litte
programs but i have now a big problem.
I have (Win-)Files which contains something like

fjioejfejfkljklTCHAT "Blablabla" kfoefeisdfidjsfmiosdf...TCHAT "Blablabla"
....

(tchat are also in low letters)

So i want to replace all Blablabla's with a some chars (i did choose *)
So it should look like

fjioejfejfkljklTCHAT "*********" kfoefeisdfidjsfmiosdf


So I wrote the following Program.

def demorem():
    filea = os.stat('c:/tmp/test.dat')[ST_SIZE]
    print "Filesize:" , filea , "Bytes"
    print "Please wait..."
    f=open('c:/tmp/test.dat', 'r+')
    fpos= int(0)
    while fpos < filea:
        f.seek(fpos)
        fstr=f.read(5)
        if fstr == 'tchat':
                print "tchat found"
                fpos=fpos+7
                f.seek(fpos)
                fstr2=f.read(1)
                i=int(0)
                ssign='"'
                while fstr2 != ssign :
                    f.write('*')
                    print fstr2, "-> *"
                    i=i+1
                    if i > 200 :
                        print "Error: someone said too much"
                        break
                    fpos=fpos+1
                    f.seek(fpos)
                    fstr2=f.read(1)
       fpos = fpos+1
 f.close()
 print "Done."


I also use the following at the beginning.

import os, sys
from stat import *

I don´t know if i really need them both, but i will try it later. :)

Now my Problem. If i start my program then it seems that all is working.
"tchat found"
the chars which sould be replaced are listet correct on my screen

tchat found
X -> *
Y -> *
...

But my file is still the old one, nothing were replaced.
Why does this
f.write('*')
nothing do to my file?

And after my program is halted, my Test-File is still in use, said Windows
when i wanted to modify it. Is
f.close()
also wrong?

It would be great if you could help me.
bye
Ingo Siebert





More information about the Python-list mailing list