32.9k views
1 vote
Which Python function allows you to execute Linux shell commands in Python?

User Moki
by
8.0k points

1 Answer

3 votes

Final answer:

The function to execute Linux shell commands in Python is subprocess.run(). It is part of the subprocess module, and subprocess.Popen() can also be used for more complex tasks.

Step-by-step explanation:

The Python function that allows you to execute Linux shell commands is subprocess.run(). This function is a part of the subprocess module, which must be imported before it can be used. To execute a shell command, you should pass the command and its arguments as a list to the subprocess.run(). Here is an example:

import subprocess
subprocess.run(['ls', '-l'])

You can also use subprocess.Popen() for more complex scenarios that require interaction with the process or if you need to capture the output.

User Giuseppe Schembri
by
8.3k points