lads lads - 4 months ago 8
Python Question

Run a command from Windows from Python code

How can I run the following command from Python3 on Windows 7

gcc main.cpp -o main.out
./main.out


The purpose is to execute the main.cpp file from Python3.

Answer

look subprocess call

import subprocess
subprocess.run(["gcc", "main.cpp -o main.out"])
subprocess.run(["./main.out"])

should work. But subprocess have more utilities that will be usefull for you.

Comments