1

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.

1

1 Answer 1

3

You could use subprocess.getoutput.

subprocess.getoutput("ls -l")

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 If you want to get a password from a user, it is better to use getpass module
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!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.