Ask Your Question
1

How can I execute Ubuntu commands in Python?

asked 2022-10-27 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-06-19 10:00:00 +0000

bukephalos gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2022-10-27 11:00:00 +0000

Seen: 8 times

Last updated: Jun 19 '22