[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: File contents



On Tue, May 07, 2002 at 06:21:18PM +1000,
 Craig Sanders <cas@taz.net.au> wrote 
 a message of 27 lines which said:

> does python have an edit-in-place command line option, like perl's -i?

Unfortunately no, but you can easily write a script which will do the
same. Here is an example, quick and dirty, but doing a part of the job
of -i:

#!/usr/bin/python

import shutil
import sys
import re

file = sys.argv[1]
backup = file + '.bak'
pattern = re.compile (sys.argv[2])
replacement = sys.argv[3]

shutil.copyfile (file, backup)
backup_h = open (backup, "r")
file_h = open (file, "w")
line = backup_h.readline()

while line:
    line = pattern.sub (replacement, line)
    file_h.write (line)
    line = backup_h.read()
    
file_h.close ()
backup_h.close ()


-- 
To UNSUBSCRIBE, email to debian-isp-request@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org



Reply to: