Advertisement
  1. Code
  2. Python

How to Run Unix Commands in Your Python Program

Scroll to top
Read Time: 3 min

Unix is an operating system which was developed in around 1969 at AT&T Bell Labs by Ken Thompson and Dennis Ritchie. There are many interesting Unix commands we can use to carry out different tasks. The question is, can we use such commands directly within a Python program? This is what I will show you in this tutorial.

The Unix command ls lists all files in the directory. If you put ls as is in a Python script, this is what you will get when you run the program:

1
Traceback (most recent call last):
2
  File "test.py", line 1, in <module>
3
    ls

4
NameError: name 'ls' is not defined

This shows that the Python interpreter is treating ls as a variable and requires it to be defined (i.e. initialized), and did not treat it as a Unix command.

os.system()

One solution to this issue is to use os.system() from Python’s os module.

As mentioned in the documentation, os.system():

Executes the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations.

So we can run the ls command in Python as follows:

1
import os
2
os.system('ls')

This will return the list of files in your current directory, which is where your .py program is located. My current directory looks like this:

1
env  unixfile.py  workwithexcel

Let’s take another example. If you want to return the current date and time, you can use the Unix command date as follows:

1
import os
2
os.system('date')

In my case, this was what I got as a result of the above script:

1
Thu 24 Apr 2022 10:42:41 AM EAT

call()

Although os.system() works, it is not recommended as it is considered a bit old and deprecated. A better solution is the call(args) function from Python’s subprocess module. As mentioned in the documentation about this function:

Run the command described by args. Wait for command to complete, then return the returncode attribute.

If we want to run the ls Unix command using this method, we can do the following:

1
from subprocess import call
2
call('ls')

Let’s see how we can return the date using the subprocess module, but let’s make the example more interesting.

1
import subprocess 
2
time = subprocess.Popen('date', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
3
output, err = time.communicate()
4
print('Todays date is ', output)

The above example can be run more simply using check_output(), as follows:

1
import subprocess 
2
time = subprocess.check_output('date')
3
print('Todays date is', time)

The output of the above script is:

1
Todays date is b'Thu 24 Apr 2022 10:48:19 AM EAT\n'

The above examples show the flexibility in using different subprocess functions, and how we can pass the results to variables in order to carry out further operations.

Conclusion

As we saw in this tutorial, Unix commands can be called and executed using the subprocess module, which provides much flexibility when working with Unix commands through its different functions. You can learn more about this module and its different functions in the Python documentation.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.