Python Forum

Full Version: Passing string args to Popen
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
I want to execute this command via Popen in my python script

Quote: diff --old-line-format="" --new-line-format="%5dn> %L" --unchanged-line-format="" /tmp/tmp923MGb/base.php /tmp/tmp923MGb/Test2.php

But cannot seem to get the syntax right - I have tried many combinations.

I would be grateful if someone could show me the way

Many thanks
Have you tried
['diff', '--old-line-format=""', '--new-line-format="%5dn> %L"',
'--unchanged-line-format=""',
'/tmp/tmp923MGb/base.php', '/tmp/tmp923MGb/Test2.php']
Hi
Thanks for the reply

This is an improvement though not quite right.
When the diff command is executed from the command line the arguments --old-line-format="" means that the old line is not displayed.

When run from within the python script I get a double quote for the old lines and the unchanged lines

To overcome this I amended the command to '--old-line-format='

This now gives the expected output

Thanks again
(Jan-16-2018, 08:46 AM)CardBoy Wrote: [ -> ]To overcome this I amended the command to '--old-line-format='
I hesitated to suggest this. Python has a function shlex.split() to split command lines. When I use it with the original command line it gives
>>> s = '''diff --old-line-format="" --new-line-format="%5dn> %L" --unchanged-line-format="" /tmp/tmp923MGb/base.php /tmp/tmp923MGb/Test2.php'''
>>> s
'diff --old-line-format="" --new-line-format="%5dn> %L" --unchanged-line-format="" /tmp/tmp923MGb/base.php /tmp/tmp923MGb/Test2.php'
>>> import shlex
>>> shlex.split(s)
['diff', '--old-line-format=', '--new-line-format=%5dn> %L', '--unchanged-line-format=', '/tmp/tmp923MGb/base.php', '/tmp/tmp923MGb/Test2.php']
You see that the double quotes are also removed for the --new-line-format argument. This may be the correct way to get arguments for a given command line.