artemisia480
artemisia480

Reputation: 107

How to get shell output into python after it has run the bash command

Anyone know how to get the output of a python bash code back into python?

I am running:

import subprocess
output = subprocess.run("ls -l", shell=True, stdout=subprocess.PIPE, 
universal_newlines=True)
print(output.stdout)

how do I get the output of ls -l back into my python code? I can have it dump into a file and then call on that file in my python code but is there an easier way, where my code can then directly read the output, without the additional file?

Thank you in advance.

Upvotes: 1

Views: 1101

Answers (1)

Luke
Luke

Reputation: 774

You could use subprocess.getoutput.

subprocess.getoutput("ls -l")

The function returns a string, which then you can parse.

Upvotes: 3

Related Questions