Python Forum
Passing string args to Popen
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing string args to Popen
#1
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
Reply
#2
Have you tried
['diff', '--old-line-format=""', '--new-line-format="%5dn> %L"',
'--unchanged-line-format=""',
'/tmp/tmp923MGb/base.php', '/tmp/tmp923MGb/Test2.php']
Reply
#3
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
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to compare two parameters in a function that has *args? Milan 4 1,183 Mar-26-2023, 07:43 PM
Last Post: Milan
  Passing string functions as arguments Clunk_Head 3 1,208 Jun-15-2022, 06:00 AM
Last Post: Gribouillis
  *args implementation and clarification about tuple status amjass12 10 3,928 Jul-07-2021, 10:29 AM
Last Post: amjass12
  [SOLVED] Good way to handle input args? Winfried 2 2,004 May-18-2021, 07:33 PM
Last Post: Winfried
  Two Questions, *args and //= beginner721 8 3,422 Feb-01-2021, 09:11 AM
Last Post: buran
  passing ver to os.popen evilcode1 3 2,740 Jan-26-2021, 12:32 PM
Last Post: buran
  how to pass the interactive string to Popen subprocess maiya 1 1,846 Sep-18-2020, 09:36 PM
Last Post: Larz60+
  does yield support variable args? Skaperen 0 1,641 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 2,200 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Using function *args to multiply multiple arguments allusernametaken 8 5,951 Nov-20-2019, 12:01 AM
Last Post: allusernametaken

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020