Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to modify a file 'in place' ?

Reply
Thread Tools

How to modify a file 'in place' ?

 
 
Elby
Guest
Posts: n/a
 
      07-22-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

 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      07-22-2005
Elby wrote:
> 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
 
Reply With Quote
 
 
 
 
Chris Connett
Guest
Posts: n/a
 
      07-23-2005
There is a module in the library called fileinput, should do what
you're looking for.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to modify the source of a python file inside a python egg file? yinglcs@gmail.com Python 2 03-12-2007 12:33 PM
can I modify a file without using a temporary file Lin Jingxian Perl Misc 2 05-09-2005 01:30 PM
Modify Date of a Router Configuration file kiranreddyd@gmail.com Cisco 11 01-11-2005 03:56 PM
How to modify a file using C++ file objects Venkat C++ 12 01-08-2004 08:54 AM



Advertisments