Home>Article>Backend Development> Ways to run Unix commands in your Python program
Unix is an operating system developed by Ken Thompson and Dennis Ritchie at AT&T Bell Laboratories around 1969. There are many interesting Unix commands we can use to perform different tasks. The question is, can we use such a command directly in a Python program? That's what I'm going to show you in this tutorial.
Unix Commandsls
List all files in a directory. If you putls
as-is into a Python script, you will get the following results when you run the program:
Traceback (most recent call last): File "test.py", line 1, inls NameError: name 'ls' is not defined
This indicates that the Python interpreter treatsls
as a variable and requires it to be defined (i.e. initialized), and does not treat it as a Unix command.
One solution to this problem is to useos.system()
from the Python os module.
As mentioned in the documentation,os.system()
:
Execute command (string) in subshell. This is accomplished by calling the standard C function system(), and has the same limitations.
So we can run thels
command in Python as follows:
import os os.system('ls')
This will return a list of files in the current directory, which is where the.py
program is located. My current directory looks like this:
env unixfile.py workwithexcel
Let’s give another example. If you want to return the current date and time, you can use the Unix commanddate
as follows:
import os os.system('date')
In my case, this is what I got with the above script:
Thu 24 Apr 2022 10:42:41 AM EAT
Althoughos.system()
will work, it is not recommended as it is considered somewhat old and deprecated. A better solution is thecall(args)
function in the Python subprocess module. As mentioned in the documentation about this feature:
Run the command described by args. Wait for the command to complete, then return the returncode attribute.
If we want to runls
Unix commands using this method, we can do the following:
from subprocess import call call('ls')
Let's see how to return a date using thesubprocess
module, but let's make the example more interesting.
import subprocess time = subprocess.Popen('date', stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, err = time.communicate() print('Todays date is ', output)
The above example can be run more simply usingcheck_output()
as shown below:
import subprocess time = subprocess.check_output('date') print('Todays date is', time)
The output of the above script is:
Todays date is b'Thu 24 Apr 2022 10:48:19 AM EAT\n'
The above example shows the flexibility of using differentsubprocess
functions and how we can pass the results to variables for further operations.
As we have seen in this tutorial, Unix commands can be called and executed using thesubprocess
module, which provides a lot of flexibility in using Unix commands through its different features. You can learn more about this module and its different functions in the Python documentation.
The above is the detailed content of Ways to run Unix commands in your Python program. For more information, please follow other related articles on the PHP Chinese website!