Execute commands from the command line in a python program
Tags: python, beginners, automation
Published: 2021-05-16 00:15:02

While working in Python you may come across cases where you want a quick way to run programs from the command line. This could be your own python scripts or any other command line program. This is where the subprocess module can come into play.

The subprocess module, present in a standard installation of Python is used to run new applications or programs through Python code by creating new processes.

It gives us the ability to:

1. spawn new processes
2. connect to their input, output and error pipes
3. obtain their return codes

A common use for subprocess would be to list files and folders in a directory. This makes use of the run method of subprocess.

The code will differ depending on what operating system you are on. For example, on a windows based computer this would be using the command dir

####For Linux 

```
import subprocess

subprocess.run('ls')
```

####For Windows 

```
import subprocess

subprocess.run("dir", shell=True)
```


In the code above we have:

* import subprocess module
* call the run module from it
* pass the list directory command based on your system (ls/dir). If you are on Windows you will have to additionally pass shell=True because dir is a shell command and you need to tell the system that you want to use it.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ktbztkgwui1l4bc38m6.png)
 
We automatically got output in the terminal even though we did not print it ourselves. That is how the run command works – it does not capture our command output by default.

Let’s see what happens if we try to capture our command by placing it in a variable

```
import subprocess

result = subprocess.run("dir", shell=True)

print(result)
```

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuckqzwhx0yeeqqhycyu.png)


The output is not what you would expect. We get the output still, and a message containing the arguments that were passed and the return code.

If we wanted to capture the output we would modify out code to this:

```
import subprocess

result = subprocess.run(["dir"], shell=True, capture_output=True, text=True)

print(result.stdout)
```

We have added:

* `capture_output=True`: to capture the output
* `text=True`: to decode the output to a readable format since it is captured as bytes
* We then `print result.stdout` : the result as standard output 


![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eq1e0kd1boar6gjp9i5c.png)

The same can be achieved with the follow code:

```
import subprocess

result = subprocess.run(["ls", "-la"], stdout=subprocess.PIPE, text=True)

print(result.stdout)
```


The difference is stdout=subprocess.PIPE This code performs the same function as capture_output=True

In addition, we use the ls command with the argument of -la both passed as a list 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9o25vqc15g63ipthk90.png)

###There's more..  
There's still more you can do with subprocess including outputting to a file, reading data from a file, and to trigger programs interactively to name a few things.  Find out all of these and more at: [Full Article at PythonHowToProgam](https://pythonhowtoprogram.com/use-subprocess-to-execute-commands-in-python-3/?utm_source=dev.to&utm_medium=cp&utm_campaign=ctalink&utm_term=subprocess-article)