avatarBetter Everything

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3274

Abstract

s="hljs-attribute">20</span>-<span class="hljs-number">02</span>-<span class="hljs-number">2023</span> <span class="hljs-number">11</span>:<span class="hljs-number">29</span> <DIR> .. <span class="hljs-attribute">20</span>-<span class="hljs-number">02</span>-<span class="hljs-number">2023</span> <span class="hljs-number">09</span>:<span class="hljs-number">37</span> <span class="hljs-number">106</span> help_functions.py <span class="hljs-attribute">20</span>-<span class="hljs-number">02</span>-<span class="hljs-number">2023</span> <span class="hljs-number">09</span>:<span class="hljs-number">37</span> <span class="hljs-number">121</span> main.py <span class="hljs-attribute">20</span>-<span class="hljs-number">02</span>-<span class="hljs-number">2023</span> <span class="hljs-number">09</span>:<span class="hljs-number">28</span> <DIR> pycache <span class="hljs-attribute">2</span> File(s) <span class="hljs-number">227</span> bytes <span class="hljs-attribute">3</span> Dir(s) <span class="hljs-number">850.703.937.536</span> bytes free</pre></div><h2 id="e290">Capturing a command’s output</h2><p id="8639">To assign the command’s output to a variable we can put the call of the <code>run</code> function in a variable assignment like <code>output_object = subprocess.run(..)</code>.</p><p id="3dc4">But we also have to add the <code>capture_output=True</code> argument to our function call.</p><p id="df42">And finally, to make sure the output is stored as a string instead of bytes we pass <code>text=True</code> in our functin call.</p><p id="b2c8">Then our function call looks like this:</p><div id="9918"><pre>output_object = subprocess.run(command, shell=<span class="hljs-literal">True</span>, text=<span class="hljs-literal">True</span>, capture_output=<span class="hljs-literal">True</span>)</pre></div><p id="e581">After running the code the variable <code>output_object</code> will hold a CompletedProcess instance. From this instance we can use 3 interesting attributes.</p><p id="2aa6">First, <code>returncode</code>, this has value 0 if there was no error with running the command.</p><p id="11ca">Second, <code>stdout</code>, if no error occurred this attribute will have the text output that was printed in the previous example.</p><p id="8faa">And third, <code>stderr</code>, if an error occurred this attribute will have some text about what went wrong.</p><p id="dc52">Here is a full code example for capturing the output of a command:</p><div id="86da"><pre><span class="hljs-keyword">import</span> subprocess

command = <span class="hljs-string">'dir "C:/Users/BE/Documents/Python Project"'</span> output_object = subprocess.run(command, shell=<span class="hljs-literal">True</span>, text=<span class="hljs-literal">True</span>, capture_output=<span class="hljs-literal">True</span>)

<span class="hljs-keyword">if</span> output_object.returncode == <span class="hljs-number">0</span>: <span class="hljs-built_in">print</span>(output_object.stdout) <span class="hljs-keyword">else</span>: <span class="hljs-built_in">print</span>(output_object.stderr)</pre></div

Options

<h2 id="ca5c">Running commands from a different directory</h2><p id="7c22">When using a Python program to run commands, by default they are run from the <b>current working directory</b>. That is the directory where the Python script is located.</p><p id="37d0">For example, if we did not pass the <code>"C:/Users/BE/Documents/Python Project"</code> argument to the <code>dir</code> command, it would have shown the contents of the Python script’s directory.</p><p id="660a">But we can also run commands as if we were in a different directory. To do that, pass the <code>cwd</code> argument to the <code>run</code> function and specify the path of the directory. It can be either an absolute filepath or a releative filepath.</p><p id="c5f9">To learn more about filepaths in Python you can read this short article of mine:</p><div id="923b" class="link-block">
      <a href="https://readmedium.com/filepaths-in-python-a-beginners-guide-90edfd8882a8">
        <div>
          <div>
            <h2>Filepaths in Python — A Beginner’s Guide</h2>
            <div><h3>How to specify where a file is located or what directory you want to search in? A Beginner’s Guide to working with…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*wGW6dn80chJ25Wy2I4n88w.png)"></div>
          </div>
        </div>
      </a>
    </div><p id="70c8">An example of passing a relative filepath as <b>c</b>urrent <b>w</b>orking <b>d</b>irectory to the <code>subprocess.run</code> function:</p><div id="3d1f"><pre><span class="hljs-keyword">import</span> subprocess

command = <span class="hljs-string">'dir'</span> output_object = subprocess.run(command, shell=<span class="hljs-literal">True</span>, text=<span class="hljs-literal">True</span>, capture_output=<span class="hljs-literal">True</span>, cwd=<span class="hljs-string">'Python Project'</span>)

<span class="hljs-keyword">if</span> output_object.returncode == <span class="hljs-number">0</span>: <span class="hljs-built_in">print</span>(output_object.stdout) <span class="hljs-keyword">else</span>: <span class="hljs-built_in">print</span>(output_object.stderr)</pre></div><h1 id="12f9">Thank you for reading!</h1><p id="8ee0">You can <b>get full access to all my posts by joining Medium</b>. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:</p><div id="80fc" class="link-block"> <a href="https://medium.com/@BetterEverything/membership"> <div> <div> <h2>Join Medium with my referral link — Better Everything</h2> <div><h3>Read every story from Better Everything (and thousands of other writers on Medium). Your membership fee directly…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*aa4Y_6MHVoY6Wl-9)"></div> </div> </div> </a> </div></article></body>

Run command line commands with Python

Computers come with a program called terminal or command prompt in which the user can write command line commands. When such a command is executed the computer will perform a task.

Some examples of tasks that a user can start with these commands:

  • running a Python script
  • installing a Python package
  • creating a virtual environment for Python
  • making a file or directory
  • outputting the contents of a directory
  • starting the computer program Visual Studio Code
Learn to run command line commands with Python. Image by catalyststuff on Freepik

In this article we will look at how you can run command line commands from within Python scripts. This way you don’t have to run the commands manually but can automate them.

Just 2 notes before we start:

  1. Be careful when using user input to automate commands. Users with bad intentions could send input to sabotage your system.
  2. Different operating systems have different commands to perform tasks. The commands that I show as examples might only work on Windows. But the way we run the commands should also work on other operating systems.

Importing the subprocess module

We will import the subprocess module that Python offers so that we can use its run function. To import it, just write: import subprocess

Running a command with subprocess.run

We will now run our first command. And we will use the run function for that. The command that we will write is the dir command. The linux equivalent is ls.

This command outputs the contents of a directory. As an extra argument we pass the path of the directory from which we want to see the contents.

import subprocess

command = 'dir "C:/Users/BE/Documents/Python Project"'
subprocess.run(command, shell=True)

When I use the run function I always pass along the shell=True argument. This makes it possible to write the command as 1 string and is also necessary if you are on Windows.

The function prints the output automatically:

 Volume in drive C is Windows
 Volume Serial Number is XXXXXXX

 Directory of C:\Users\BE\Documents\Python Project

20-02-2023  09:28    <DIR>          .
20-02-2023  11:29    <DIR>          ..
20-02-2023  09:37               106 help_functions.py
20-02-2023  09:37               121 main.py
20-02-2023  09:28    <DIR>          __pycache__
               2 File(s)            227 bytes
               3 Dir(s)  850.703.937.536 bytes free

Capturing a command’s output

To assign the command’s output to a variable we can put the call of the run function in a variable assignment like output_object = subprocess.run(..).

But we also have to add the capture_output=True argument to our function call.

And finally, to make sure the output is stored as a string instead of bytes we pass text=True in our functin call.

Then our function call looks like this:

output_object = subprocess.run(command, shell=True, 
                            text=True, capture_output=True)

After running the code the variable output_object will hold a CompletedProcess instance. From this instance we can use 3 interesting attributes.

First, returncode, this has value 0 if there was no error with running the command.

Second, stdout, if no error occurred this attribute will have the text output that was printed in the previous example.

And third, stderr, if an error occurred this attribute will have some text about what went wrong.

Here is a full code example for capturing the output of a command:

import subprocess

command = 'dir "C:/Users/BE/Documents/Python Project"'
output_object = subprocess.run(command, shell=True, 
                            text=True, capture_output=True)

if output_object.returncode == 0:
    print(output_object.stdout)
else:
    print(output_object.stderr)

Running commands from a different directory

When using a Python program to run commands, by default they are run from the current working directory. That is the directory where the Python script is located.

For example, if we did not pass the "C:/Users/BE/Documents/Python Project" argument to the dir command, it would have shown the contents of the Python script’s directory.

But we can also run commands as if we were in a different directory. To do that, pass the cwd argument to the run function and specify the path of the directory. It can be either an absolute filepath or a releative filepath.

To learn more about filepaths in Python you can read this short article of mine:

An example of passing a relative filepath as current working directory to the subprocess.run function:

import subprocess

command = 'dir'
output_object = subprocess.run(command, shell=True, 
                            text=True, capture_output=True,
                            cwd='Python Project')

if output_object.returncode == 0:
    print(output_object.stdout)
else:
    print(output_object.stderr)

Thank you for reading!

You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:

Programming
Software Development
Software Engineering
Python
Command Line
Recommended from ReadMedium