Python as a scripting language. Alternative to bash script?

Benjamin Kaplan benjamin.kaplan at case.edu
Mon Jun 28 10:58:12 EDT 2010


On Mon, Jun 28, 2010 at 4:48 AM, Dave Pawson <dave.pawson at gmail.com> wrote:
> I've a fairly long bash script and I'm wondering
> how easy it would be to port to Python.
>
> Main queries are:
> Ease of calling out to bash to use something like imageMagick or Java?

Easiest way is os.system, most flexible way is subprocess.Popen.

> Ease of grabbing return parameters? E.g. convert can return both
> height and width of an image. Can this be returned to the Python program?

How does a program return anything other than an exit code? Subprocess
allows you to read the program's stdout if that's what you're looking
for. In the case of ImageMagick, you can use a Python wrapper to the
library instead of calling the program from the command line, and then
you can get all the return values you want.

> Can Python access the exit status of a program?

proc = subprocess.Popen(args)
retcode = proc.wait()

There's a shortcut of
retcode = subprocess.call(args), but that doesn't give you access to
stdout, just the return code.
>
> I'd prefer the advantages of using Python, just wondering if I got
> so far with the port then found it wouldn't do something?
>

If there's anything it can't do that bash can, you can always just
call the shell command.

> Has anyone made this comparison please?
>

If you already have a working shell script, it's probably not worth
your time. But if you've having trouble getting bash to cooperate,
it's not that difficult to rewrite a shell script in Python.

> TIA
>
> --
> Dave Pawson
> XSLT XSL-FO FAQ.
> Docbook FAQ.
> http://www.dpawson.co.uk
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list