What I want to do is run a command from an .exe in the Windows command prompt like this
...\test.exe cmd
test
The idea is that on the command prompt, you writer “test.exe cmd” and then the program will print “test”. I’ve had a go at this myself and tested it, then failed miserably.
import sys # Imports the 'sys' module, which allows access to the command line arguments.
import subprocess # Imports the 'subprocess' module, which allows you to run external commands.
if len(sys.argv) != 3:
print("Usage: test.exe <command>")
sys.exit(1)
executable = sys.argv[1] # path to the .exe
command = sys.argv[2] # command
if command == "test":
try:
subprocess.run([executable, command], check=True)
print("test")
except subprocess.CalledProcessError as e:
print(f"Error running {executable}: {e}")
sys.exit(1)
All it does is display “Usage: test.exe ” so far.