Executing a command from within python using the subprocess module

Peter Otten __peter__ at web.de
Mon Feb 15 14:02:42 CET 2010


R (Chandra) Chandrasekhar wrote:

> I want to execute a command from within python using the subprocess
> module.
> 
> Coming from a Perl background, I thought I could use variable
> interpolation in strings, but found that this is neither supported nor
> the Python way. Accordingly, I am a little at sea about how to
> accomplish it.

import subprocess

def convert(width=5, height=30, colors=['#abcdef', '#456789'],
            filename="tmp/image with space in its name.png"):
    lookup = locals()
    assert all("\n" not in str(s) for s in lookup.values())
    subprocess.call("""\
convert
-size
{width}x{height}
gradient:{colors[0]}-{colors[1]}
{filename}""".format(**lookup).splitlines())

convert()

Peter



More information about the Python-list mailing list