import subprocess subprocess.call("command1") subprocess.call(["command1", "arg1", "arg2"])
import subprocess subprocess.call(["ls", "-l"])
total 4 -rw-rw-r-- 1 tevin tevin 15 Sep 3 15:29 test.txt
For more flexibility, you can use Popen . Using this you can store the output of command as well as any error occurred during command execution.
process = subprocess.Popen(["ls", "-l"]) (output, err) = process.communicate()
communicate method interacts with process and waits for the process to complete. It returns a tuple consisting of stdout and stderr.
Executing commands in background:
subprocess.Popen() only runs a process in the background if nothing in the python script depends on the output of the command being run. For example, the following code won’t be executed in background.
import subprocess process = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE)
Thinking about what is subprocess.PIPE?
subprocess.PIPE: Special value that can be used as the stdin, stdout or stderr argument to Popen and indicates that a pipe to the standard stream should be opened.
Changing directory(cd command):
You might be thinking that why ‘cd’? Why it can’t be executed using subprocess?
If you use subprocess.call(“cd ..”), it will throw an error ‘No such file or directory’. It is because cd is a shell internal. So you can only call it as
subprocess.call('cd ..', shell=True)
But it is pointless to do so. As no process can change another process’s working directory (again, at least on a UNIX-like OS, but as well on Windows), this call will have the subshell change its dir and exit immediately. Don’t worry you can change directory using os.chdir(path).