You can execute Ubuntu commands in Python by using the subprocess module. Here's an example:
import subprocess
result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)
print(result.stdout.decode('utf-8'))
In this example, the subprocess.run()
function is used to execute the Ubuntu command ls -l
. The stdout=subprocess.PIPE
parameter indicates that the output of the command should be captured and returned as a byte string. Finally, we decode the output using the decode()
method to convert the bytes to a human-readable string.
You can replace ls -l
with any Ubuntu command you want to execute in Python.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-10-27 11:00:00 +0000
Seen: 8 times
Last updated: Jun 19 '22