Executing a command from within python using the subprocess module
Steve Holden
steve at holdenweb.com
Mon Feb 15 11:31:58 EST 2010
R (Chandra) Chandrasekhar wrote:
> 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.
It does, but a leading newline would cause the splitlines() result to
start with an empty word - the backslash just causes the interpreter to
ignore the newline immediately following, rather than including it in
the string literal's value.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/
More information about the Python-list
mailing list