gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
How to possible run "program --option" with subprocess.Popen() in Python?
- User
How to possible run "program --option" with subprocess.Popen() in Python?
- Assistant
To run a command-line program with options (like
program --option) usingsubprocess.Popen()in Python, you need to pass the command and its options as a list of strings. The first string in the list will be the name of the program, and the subsequent strings will be the options and arguments.Here's an example of how to do this:
import subprocess # Define the command and options command = ["program", "--option"] # Run the command process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Wait for the process to complete and capture output and errors if any stdout, stderr = process.communicate() # Print the output and errors print("Output:", stdout.decode()) print("Error:", stderr.decode())