Executing a command from within python using the subprocess module

R (Chandra) Chandrasekhar chyavana at gmail.com
Mon Feb 15 17:11:36 CET 2010


Peter Otten wrote:
> 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

One other question I forgot to ask is this why is there a terminal 
backslash in

> subprocess.call("""\

Removing the backslash makes the function fail.

I wonder why, because """ is supposed to allow multi-line strings. I am 
sorry if this sounds obtuse but that backslash baffles me.



More information about the Python-list mailing list