How to execute commands of the unix based operating system in Python

Every operating system I am aware of comes with a command line utility through which the wise computer geek can execute commands and interact with the functionalities provided by it. Based on my personal experience with computers so far, Unix based machines offer a terminal utility to help the user run specific builtin commands which interact with the goodies offered by the operating system. On the other hand, Windows based machines come with what is known as CMD, similar to the terminal tool offered in Unix based operating systems.

Both the Unix terminal and the Windows CMD utilities, are being built to help the computer nerd get the most out of their own operating system. Before one can fully make use of these utilities, they should learn about the scripting language supported by them.

The Windows CMD and the Unix based terminal function based on a scripting language. There are many scripting languages out there which can be used to interact with the functionalities provided by the operating system. Python is one of them, but you should know that such computer technology is totally different of the scripting languages supported by the CMD and the Unix based terminal.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Through this article, you are going to learn how to make use of the Python computer programming language to execute commands which are specific to the Unix based operating system. Codetheory.in is going to teach its readers how they can run Windows based commands through the Python technology in another article.

Before going any further, make sure to open a fresh terminal and a new interactive Python shell on your own operating system, so you can practice the code by yourself and get better with it.

Once you have managed to launch both the terminal utility and the fresh Python interactive console, make sure to run the following Unix based command on the terminal utility, with the main purpose of creating a new directory in your own computer.

mkdir commands

Once the above Unix based command gets executed in your own operating system, a new directory called commands will be created.

Then use the following command to delete the directory you just created.

rm -r commands

Both the above commands are Unix based commands. The first one is used to create a new directory on Unix based systems. On the other hand, the second command is used to remove a directory and its entire tree.

Being rich in features and utilities, the Python computer programming language can be easily used to achieve the same results we accomplished by using the Unix based commands shown above.

The Python utility which we need for the purpose of this article, is the one being shown below.

os.system

As you may already know, before one can make use of the Python builtin utilities found in its modules, they have to make use of the import statement like shown in the following example.

from os import system

According to the Python official documentation, the os.system utility is being used to execute a command in a subshell. Based on my personal experience with this Python utility, the command one wants to execute through it, must be a string object.

To get practical, let’s define the following command.

cmd = ‘python –version’

Once you have managed to declare the above string object in your own Python interactive console, run the following command to execute it. For those of you who don’t know, when the above command is executed in a Unix based console, the output which comes out holds the version of the Python interpreter installed on the machine.

system(cmd)

Once the above Python command is executed on my interactive console, the following output comes out.

Python 2.7.12

The above output informs the user that they’re making use of the Python 2.7.12 version.

Finally you have managed to successfully run your first Unix based command through the os.system Python utility.

Now declare the command shown below, a string object which helps to create a new directory on Unix based machines.

cmd = ‘mkdir commands’

The same way we executed the first command through the os.system Python utility, we are going to execute the above command in our Unix based machines.

system(cmd)

To make sure that the above command has been executed successfully and that our new directory called commands has been created, we need to list all the files inside the current working directory by making use of the ls Unix based command.

Before we can do that, we need to define the string object for the command which we are going to execute.

list_files = ‘ls’

Then list the files through the Python os.system utility by making use of the command shown below.

system(list_files)

Once the above Python command is executed in your own Unix based operating system, the list of files inside the current working directory will get printed on the console. Among the files part of the current working directory, must be the subdirectory which you created through the Python os.system utility.

Having some experience with the Unix based commands and the terminal utility, I am going to share a very simple hack which can automate the process of looking for the subdirectory.

Based on my wisdom on the Unix based operating system, a utility which can search any input files is called grep.

What we can do, is to redirect the output produced by the ls utility, to the grep one and then search for the subdirectory called commands.

So let’s define a new command by using a Python string object like shown in the following example.

search_commands = ‘ls | grep commands’

Once you have managed to define the above Python string object in your interactive console, make sure to run the following piece of Python code to execute it.

system(search_commands)

After the above piece of Python code gets executed on my interactive shell, the following output comes on the console.

commands

When the Unix based utility called grep finds the input it checks for, it returns it as an output on the console. Since the Python command which we executed returned the subdirectory commands as an output in the console, it means that our subdirectory has been created.

What if the Python coder wants to remove a subdirectory with the help of the os.system utility? It is not that hard!

What we need to do is to define the command as a Python string object and pass it as an argument to the os.system utility.

Define the Unix based command which helps one to remove a directory like shown below.

remove_directory = ‘rm -r commands’

Based on the above Unix based command, defined as a Python string object, the reader can easily understand that the directory which we want to remove is the commands one.

Then execute the following Python based command to remove the directory commands.

system(remove_directory)

Final thoughts

Being a computer programming language full of goodies and utilities, Python offers many different ways to interact with the functionalities of the operating system. Through this article, we practiced a few Unix based commands and learned how to run them through Python by making use of the builtin os.system utility. Although a better option to run os commands through Python is the subprocess module, for the unexperienced geek the os.system utility is the perfect place to start as it is very simple to understand and use.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *