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 put ls
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, in <module> ls NameError: name 'ls' is not defined
This indicates that the Python interpreter treats ls
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 use os.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 the ls
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 command date
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
Although os.system()
will work, it is not recommended as it is considered somewhat old and deprecated. A better solution is the call(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 run ls
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 the subprocess
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 using check_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 different subprocess
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 the subprocess
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!