0

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.

artemisia480
  • 77
  • 2
  • 7
  • Possible duplicate of [Running Bash commands in Python](https://stackoverflow.com/questions/4256107/running-bash-commands-in-python) – tripleee Dec 07 '18 at 20:21

1 Answers1

2

You could use subprocess.getoutput.

subprocess.getoutput("ls -l")

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

Luke
  • 744
  • 2
  • 7
  • 23
  • Thank you! The thing is the output will be a password! Which then I am feeding to another method for authentication. Can I parse a password, will it still work? – artemisia480 Dec 07 '18 at 21:32
  • 1
    @artemisia480 If you want to get a password from a user, it is better to use `getpass` module – rth Dec 07 '18 at 23:04
  • no it's not a password from the usr, the link I am running generates a password. I have updated the question, hope that makes things more clear. Thank you again everyone!!! – artemisia480 Dec 07 '18 at 23:13