How to modify a file 'in place' ?

Discussion in 'Python' started by Elby, Jul 22, 2005.

  1. Elby

    Elby Guest

    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
     
    Elby, Jul 22, 2005
    #1
    1. Advertising

  2. Elby

    John Machin Guest

    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
     
    John Machin, Jul 22, 2005
    #2
    1. Advertising

  3. There is a module in the library called fileinput, should do what
    you're looking for.
     
    Chris Connett, Jul 23, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Venkat
    Replies:
    12
    Views:
    10,671
    Chris \( Val \)
    Jan 8, 2004
  2. Daniel Nogradi

    loop over list and modify in place

    Daniel Nogradi, Sep 30, 2006, in forum: Python
    Replies:
    5
    Views:
    347
    Paul Rubin
    Oct 1, 2006
  3. Replies:
    2
    Views:
    882
    Jorge Godoy
    Mar 12, 2007
  4. Replies:
    3
    Views:
    533
    Steven D'Aprano
    Jul 1, 2011
  5. Lin Jingxian
    Replies:
    2
    Views:
    99
    Tad McClellan
    May 9, 2005
Loading...

Share This Page