How to modify a file 'in place' ?

Elby berthel4 at cti.ecp.fr
Fri Jul 22 12:05:32 EDT 2005


I'm looking for a the most simple and generic way to modify a file, with the
possibility of making backups. In fact, I would like to emulate Perl's -i
option.
        
here is a bit of code, to explain it further :

< code >

from os import rename

class Modif_File:
    def __init__(self, filename, ext='.bak'):
        old_name = filename + ext
        new_name = filename
        rename(new_name,old_name)
        
        self.old = open(old_name,'r')
        self.new = open(new_name,'w')
        
        # methods for getting data are linked to the old file :
        for attr in ('encoding', 'newlines', 'next', 'read',
                    'readinto', 'readline', 'readlines', 'seek', 
                    'tell', 'xreadlines'):
            setattr(self,attr,getattr(self.old,attr))
        
        # methods for putting data are linked to the new one :
        for attr in ('closed','flush','write', 'writelines'):
            setattr(self,attr,getattr(self.new,attr))
        
    def close(self):
        self.new.close()
        self.old.close()

</ code >

for example, an equivalent of 
        perl -i.bak -pe 's/\t+$//' *txt
could be :

< code >

from glob import glob
from re import compile, MULTILINE

regex = compile(r'\t+$',MULTILINE)

for f in [Modif_File(name) for name in glob('*.txt')]:
    f.write(regex.sub('',f.read()))
    f.close()

</ code >

Of course, this example is very basic and my class Modif_File does not take
into account :
  - the right of the file
  - the mode of the file (binairy/text)
  - ...etc

What is the best way to do it ?

-- 
Elby  




More information about the Python-list mailing list