"perl -p -i -e" trick in Python?

Jack Diederich jack at performancedrivers.com
Wed Feb 16 01:44:40 EST 2005


On Wed, Feb 16, 2005 at 04:38:03PM +1000, Stephen Thorne wrote:
> On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee <shotgunlee at hotmail.com> wrote:
> > I read the comment of
> > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277753.
> > (Title : Find and replace string in all files in a directory)
> > 
> > "perl -p -i -e 's/change this/..to this/g'" trick looks handy.
> > Does Python have a similar trick? Or, is there a shorter Python recipe for
> > the given problem?
> 
> sure.
> 
> python -c 'import os; os.system("sed -i s/change this/...tothis/g")'

You beat me to it, but you can cut a few more characters out of that.

/tmp/> python
Python 2.3.4 (#2, Jan  5 2005, 08:24:51) 
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
/tmp/> sed -i 's/change this/...tothis/g'


-Jack



More information about the Python-list mailing list