Re: replace string in a file

2008-03-17 Thread cantabile
Le Mon, 17 Mar 2008 09:03:07 -0700, joep a écrit : > An example: looks for all 'junk*.txt' files in current directory and > replaces in each line the string 'old' by the string 'new' > Josef Works like a charm. Many thanks for the example Josef :-) -- http://mail.python.org/mailman/listinfo/pyth

Re: replace string in a file

2008-03-17 Thread joep
On Mar 16, 10:35 pm, sturlamolden <[EMAIL PROTECTED]> wrote: > On 15 Mar, 21:54, Unknown <[EMAIL PROTECTED]> wrote: > > > I was expecting to replace the old value (serial) with the new one > > (todayVal). Instead, this code *adds* another line below the one found... > > > How can I just replace it?

Re: replace string in a file

2008-03-16 Thread sturlamolden
On 15 Mar, 21:54, Unknown <[EMAIL PROTECTED]> wrote: > I was expecting to replace the old value (serial) with the new one > (todayVal). Instead, this code *adds* another line below the one found... > > How can I just replace it? A file is a stream of bytes, not a list of lines. You can't just rep

Re: replace string in a file

2008-03-16 Thread Unknown
Le Sat, 15 Mar 2008 16:55:45 -0700, joep a écrit : > you can also use standard module fileinput.input with ''inplace'' option > which backs up original file automatically. > > from python help for fileinput: > > Optional in-place filtering: if the keyword argument inplace=1 is passed > to input(

Re: replace string in a file

2008-03-15 Thread joep
you can also use standard module fileinput.input with ''inplace'' option which backs up original file automatically. from python help for fileinput: 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 f

Re: replace string in a file

2008-03-15 Thread Unknown
Thanks, andrei. I'll try that. Le Sat, 15 Mar 2008 14:25:21 -0700, andrei.avk a écrit : > What you want to do is either 1. load everything up into a string, > replace > text, close file, reopen it with 'w' flag, write string to it. OR if > file is too big, you can read each line, replace, write t

Re: replace string in a file

2008-03-15 Thread andrei . avk
On Mar 15, 3:54 pm, Unknown <[EMAIL PROTECTED]> wrote: > Hi, > I've got this code : > > cb = open("testfile", "r+") > f = cb.readlines() > for line in f: >     rx = re.match(r'^\s*(\d+).*', line) >     if not rx: >         continue >     else: >         serial = rx.group(1) >         now = time.tim

replace string in a file

2008-03-15 Thread Unknown
Hi, I've got this code : cb = open("testfile", "r+") f = cb.readlines() for line in f: rx = re.match(r'^\s*(\d+).*', line) if not rx: continue else: serial = rx.group(1) now = time.time() today = time.strftime('%Y%m%d00', time.localtime(now)) to