I know of python -c '<code>'
, but I'm wondering if there's a more elegant python equivalent to perl -pi -e '<code>'
. I still use it quite a bit for things like find and replace in a whole directory (perl -pi -e s/foo/bar/g *
or even find . | xargs perl -pi -e s/foo/bar/g
for sub-directories).
I actually feel that that which makes Perl Perl (free form Tim Toady-ness) is what makes perl -pi -e
work so well, while with Python you'd have to do something along the lines of importing the re module, creating an re instance and then capture stdin, but maybe there's a Python shortcut that does all that and I missed it (sorely missed it)...
The command line usage from 'python -h
' certainly strongly suggests there is no such equivalent. Perl tends to make extensive use of '$_
' (your examples make implicit use of it), and I don't think Python supports any similar concept, thereby making Python equivalents of the Perl one-liners much harder.
An equivalent to -pi isn't that hard to write in Python.
Write yourself a handy module with the -p and -i features you really like. Let's call it pypi.py
.
Use python -c 'import pypi; pypi.subs("this","that")'
You can implement the basic -p loop with the fileinput module.
You'd have a function, subs
that implements the essential "-i" algorithm of opening a file, saving the backup copy, and doing the substitute on each line.
There are some activestate recipes like this. Here are some:
Not built-in. But not difficult to write. And once written easy to customize.
I know this is a couple of years too late, but I've recently found a very nice tool called pyp, which does exactly what you've asked for.
I think your command should be:
pyp "p.replace('foo','bar')"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With