Python Forum
Pass command line argument with quotes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass command line argument with quotes
#1
I'm relatively new to Python and have googled around enough to understand in general how to use os.system and subprocess, but struggling to essentially send a command like this via my python script:

C:\Program Files\Editor\Editor.exe C:\Downloads\123.pdf

When I type that into my cmd, it opens 123.pdf with Editor.

Thanks in advance!
Reply
#2
koticphreak Wrote:but struggling to essentially send a command like this
We don't see much of the struggle. What have you tried with python code?
Reply
#3
(May-31-2019, 10:34 PM)Gribouillis Wrote: What have you tried with python code?
subprocess.call(["C:\Program Files\Editor\Editor.exe", "C:\Downloads\123.pdf"])
Reply
#4
Backslashes in strings indicate special characters, so your string does not contain the characters you think it does. There are a few ways to solve this:

subprocess.call([r"C:\Program Files\Editor\Editor.exe", "C:\Downloads\123.pdf"])      # raw string
subprocess.call(["C:\\Program Files\\Editor\\Editor.exe", "C:\\Downloads\\123.pdf"])  # escape the backslashes
subprocess.call(["C:/Program Files/Editor/Editor.exe", "C:/Downloads/123.pdf"])       # forward slashes
Forward slashes will work because they work in file paths regardless of the OS.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to pass a mongdb command to a module and execute it. cspower 0 316 Feb-03-2024, 09:54 PM
Last Post: cspower
  How to pass encrypted pass to pyodbc script tester_V 0 858 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Command line argument issue space issue mg24 5 1,340 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  accept command line argument mg24 5 1,315 Sep-27-2022, 05:58 PM
Last Post: snippsat
  Why does absence of print command outputs quotes in function? Mark17 2 1,385 Jan-04-2022, 07:08 PM
Last Post: ndc85430
Question How to pass a method as argument in an another method? anilanvesh 6 2,748 Sep-30-2021, 10:18 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 3,605 Sep-06-2021, 03:43 PM
Last Post: muzikman
  Accessing varying command line arguements Rakshan 3 2,051 Jul-28-2021, 03:18 PM
Last Post: snippsat
  How to input & output parameters from command line argument shantanu97 1 2,576 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Two types of single quotes Led_Zeppelin 2 1,917 Mar-15-2021, 07:55 PM
Last Post: BashBedlam

Forum Jump:

User Panel Messages

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