Executing a command from within python using the subprocess module

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


Alf P. Steinbach wrote:
> * R (Chandra) Chandrasekhar:
>>
>> width = 5
>> height = 30
>> colors = ['#abcdef]', '#456789']
>> filename = "/tmp/image.png"
>>
>> # I want to get the equivalent of variable interpolation in Perl
>> # so that the command
>> #
>> # convert -size 5x30 gradient:#abcdef-#456789 /tmp/image.png
>> #
>> # is derived from the variables above
> 
> Assuming that the extra right square bracket in 'colors' is a typo, 
> here's one way:
> 
>     s = "convert -size {w}x{h} gradient:{g1}-{g2} {f}".format(
>         w = width, h = height, g1 = colors[0], g2 = colors[1], f = filename
>         )
> 
> 
> Cheers & hth.,
> 
> - ALf

Thanks, Alf. It works if I use it so:

subprocess.call(s, shell=True)

and I think that is because s is a single string assembled in almost the 
variable interpolation fashion of Perl. It does not work with the 
default shell=False argument, presumably because the arguments are not 
split into separate strings (something that was horrible to do by hand 
using the %s and %() syntax that I had tried out previously).

I think that you and Peter have, between you, shown me two ways of using 
subprocess.call(): one with shell=True and the other with shell = False.

Thanks.

Chandra



More information about the Python-list mailing list