Python Forum
Passing string args to Popen - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Passing string args to Popen (/thread-7547.html)



Passing string args to Popen - CardBoy - Jan-15-2018

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


RE: Passing string args to Popen - Gribouillis - Jan-15-2018

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



RE: Passing string args to Popen - CardBoy - Jan-16-2018

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


RE: Passing string args to Popen - Gribouillis - Jan-16-2018

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