How to Run Unix Commands in Your Python Program

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:

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:

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:

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

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

call()

Although os.system() works, it is not recommended as it is considered a bit old and deprecated. A more recommended solution is Python’s subprocess module call(args) function. 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:

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

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

The output of the above scripts is:

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 from the Python documentation.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Abder-Rahman Ali

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:

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:

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:

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

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

call()

Although os.system() works, it is not recommended as it is considered a bit old and deprecated. A more recommended solution is Python’s subprocess module call(args) function. 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:

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

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

The output of the above scripts is:

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 from the Python documentation.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Abder-Rahman Ali


Print Share Comment Cite Upload Translate Updates
APA

Abder-Rahman Ali | Sciencx (2016-10-12T04:53:41+00:00) How to Run Unix Commands in Your Python Program. Retrieved from https://www.scien.cx/2016/10/12/how-to-run-unix-commands-in-your-python-program/

MLA
" » How to Run Unix Commands in Your Python Program." Abder-Rahman Ali | Sciencx - Wednesday October 12, 2016, https://www.scien.cx/2016/10/12/how-to-run-unix-commands-in-your-python-program/
HARVARD
Abder-Rahman Ali | Sciencx Wednesday October 12, 2016 » How to Run Unix Commands in Your Python Program., viewed ,<https://www.scien.cx/2016/10/12/how-to-run-unix-commands-in-your-python-program/>
VANCOUVER
Abder-Rahman Ali | Sciencx - » How to Run Unix Commands in Your Python Program. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2016/10/12/how-to-run-unix-commands-in-your-python-program/
CHICAGO
" » How to Run Unix Commands in Your Python Program." Abder-Rahman Ali | Sciencx - Accessed . https://www.scien.cx/2016/10/12/how-to-run-unix-commands-in-your-python-program/
IEEE
" » How to Run Unix Commands in Your Python Program." Abder-Rahman Ali | Sciencx [Online]. Available: https://www.scien.cx/2016/10/12/how-to-run-unix-commands-in-your-python-program/. [Accessed: ]
rf:citation
» How to Run Unix Commands in Your Python Program | Abder-Rahman Ali | Sciencx | https://www.scien.cx/2016/10/12/how-to-run-unix-commands-in-your-python-program/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.