Python Script execute commands in Terminal Python Script execute commands in Terminal python python

Python Script execute commands in Terminal


There are several ways to do this:

A simple way is using the os module:

import osos.system("ls -l")

More complex things can be achieved with the subprocess module:for example:

import subprocesstest = subprocess.Popen(["ping","-W","2","-c", "1", "192.168.1.70"], stdout=subprocess.PIPE)output = test.communicate()[0]


I prefer usage of subprocess module:

from subprocess import callcall(["ls", "-l"])

Reason is that if you want to pass some variable in the script this gives very easy way for example take the following part of the code

abc = a.ccall(["vim", abc])