Executing a command from within python using the subprocess module

Nobody nobody at nowhere.com
Mon Feb 15 17:56:51 CET 2010


On Tue, 16 Feb 2010 00:11:36 +0800, R (Chandra) Chandrasekhar wrote:

> 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.

The backslash causes the following newline to be skipped, so the "c" at
the beginning of "convert" is the first character in the string. Without
it, the newline would be the first character, and the .splitlines() would
result in an empty string as the first element in the list.

Example:

> """
= hello
= world
= """.splitlines()
['', 'hello', 'world']
> """\
= hello
= world
= """.splitlines()
['hello', 'world']





More information about the Python-list mailing list