python line editor

Art abrady at
Wed Aug 25 01:11:57 CEST 2004

Hello all,

this is possibly a not well-formed question, but are there any python
modules out there to do vi/ex/ed style script editing?

i.e. the kind where you pass the commands thru stdin. ie:

or something like that.

I'm porting some software right now so I have thousands of files to do
replaces on. I want to do everything in Python, but I find myself
writing a bunch of functions that look like this:
                    for line in fileinput.input( os.path.join( dir,
file ), inplace=1, backup="~"):
                        m = xbrex.match( line )
                        if m:
                            resdef = ""
                            #is this an ndef?
                                resdef = "#if !defined(_XBOX) &&
                                resdef = "#if defined(_XBOX) ||
                            #fixup the line
                            line = line.replace(, resdef)
                        # write the line

Basically my problem is that I have to operate on files on a line by
line basis using this method. This case works fine, but there are
cases where I would like multi-line changes, basically mini-editor

I know that this could be done with code i.e. if( startFound ): ...,
but I don't want to have to track a bunch of flags. . .

I know I could slurp the whole file into a string, but that still
doesn't have the simplicity of the ed syntax.

Since I'm basically asking for editor functionality, an alternative
would be to do this with ed and scripts, or elisp, but I'd like to try
python for this first.

Any suggestions? 

Best regards,

More information about the Python-list mailing list