How to execute a command in the terminal from a Python script? How to execute a command in the terminal from a Python script? shell shell

How to execute a command in the terminal from a Python script?


The echo terminal command echoes its arguments, so printing the command to the terminal is the expected result.

Are you typing echo driver.exe bondville.dat and is it running your driver.exe program?
If not, then you need to get rid of the echo in the last line of your code:

os.system(command)


You can use the subprocess.check_call module to run the command, you don't need to echo to run the command:

from subprocess import check_callcheck_call(["./driver.exe", "bondville.dat"])

Which is equivalent to running ./driver.exe bondville.dat from bash.

If you wanted to get the output you would use check_outout:

from subprocess import check_outputout = check_output(["./driver.exe", "bondville.dat"])

In your own code you are basically echoing the string command not actually running the command i.e echo "./driver.exe bondville.dat" which would output ./driver.exe bondville.dat in your shell.


Try this:

import subprocesssubprocess.call("./driver.exe bondville.dat")