Python Forum
Simplest way to run external command line app with parameters?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simplest way to run external command line app with parameters?
#1
Hello,

Out of curiosity, is this the simplest way in Python3 to run an external command-line application with parameters?

Thank you.

from subprocess import Popen, PIPE
import shlex

command_line = "command -a 50 -o %03d.jpg input.doc"
args = shlex.split(command_line)
args.insert(0, r"C:\app.exe")
process = Popen(args,stdout=PIPE, stderr=PIPE)

stdout, stderr = process.communicate()

print(stdout)
Reply
#2
maybe can you try something like:

import subprocess, os, sys
command_line = subprocess.Popen('command -a 50 -o %03d.jpg input.doc', 
                                 shell = True, 
                                 stdin = None, 
                                 stdout = subprocess.PIPE) 
run = subprocess.Popen('C:\app.exe', 
                       stdin = command_line.stdout, 
                       stdout = subprocess.PIPE)
result = run.communicate() 
print(result)
run.wait()
sys.stdout.flush() 
Reply
#3
A advice subprocess.run should be used with all cases it can handle.
Subprocess Doc
Quote:The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle.
For more advanced use cases, the underlying Popen interface can be used directly.
Can also use capture_output=True,and no need top use args.insert as see f-string dos this fine.
import subprocess

output_file = "%03d.jpg"
input_file = "input.doc"
executable_path = r"C:\app.exe"
command_line = f"{executable_path} command -a 50 -o {output_file} {input_file}"
args = command_line.split()
result = subprocess.run(args, encoding='utf-8', capture_output=True)
print(result.stdout)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am a newbie I like to use the command line Mikel2025 1 1,063 Jun-13-2025, 03:20 PM
Last Post: Gribouillis
  Insert command line in script lif 4 2,200 Mar-24-2025, 10:30 PM
Last Post: lif
  Command line argument issue space issue mg24 5 3,167 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  accept command line argument mg24 5 3,140 Sep-27-2022, 05:58 PM
Last Post: snippsat
  Accessing varying command line arguements Rakshan 3 3,742 Jul-28-2021, 03:18 PM
Last Post: snippsat
  How to input & output parameters from command line argument shantanu97 1 4,791 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Simplest/Quickest/Best Way to Get Started? abrogard 7 6,120 Oct-22-2020, 10:53 AM
Last Post: metulburr
  Passing List of Objects in Command Line Python usman 7 5,817 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Taking Multiple Command Line Argument Input bwdu 6 10,152 Mar-29-2020, 05:52 PM
Last Post: buran
  python 3 from command line Dixon 1 3,022 Mar-01-2020, 08:35 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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