Home > Backend Development > Python Tutorial > Ways to run Unix commands in your Python program

Ways to run Unix commands in your Python program

WBOY
Release: 2023-08-30 10:45:12
Original
1521 people have browsed it

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 CommandslsList 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
Copy after login

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.

os.system()

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')
Copy after login

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
Copy after login

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')
Copy after login

In my case, this is what I got with the above script:

Thu 24 Apr 2022 10:42:41 AM EAT
Copy after login

transfer()

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')
Copy after login

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)
Copy after login

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)
Copy after login

The output of the above script is:

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

The above example shows the flexibility of using different subprocess functions and how we can pass the results to variables for further operations.

in conclusion

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template