How to modify a file 'in place' ?

E

Elby

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 ?
 
J

John Machin

Elby said:
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)

Quite apart from unusual ideas about what "old" and "new" mean, you have
a problem if filename == "foo" and a physical file named "foo.bak"
exists already.
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))

You seem to be majorly confused between a physical file on disk and a
file object used for accessing physical files. There is absolutely
neither need nor usefulness in doing all that getattr/setattr stuff.

Have a look at the documentation for the shutil module. The functions in
that should do most/all of what you want. Then have a look at the
*source* for that module -- which will be present on your machine; on
mine it's C:\Python24\Lib\shutil.py -- and see how elementary physical
file manipulations are done, with no getattr or setattr calls.

HTH,
John
 
C

Chris Connett

There is a module in the library called fileinput, should do what
you're looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top