1

Is there a way to assign the terminal output to a varibale then print it to the screen, using os.popen()

For example:

import os
output = os.popen("dir")
print(output)

Is this possible?

5
  • 1
    Does this answer your question? Commented Dec 30, 2020 at 10:41
  • I don't want to use subprocess, is there any other way? Commented Dec 30, 2020 at 10:41
  • 1
    os.popen aready uses subprocess - doc: os.popen uses subprocess.Popen() Commented Dec 30, 2020 at 10:42
  • Do you have to assign it to a variable or just printing it on screen is enough? Commented Dec 30, 2020 at 10:42
  • @Jarvis I need to assign it to a variable Commented Dec 30, 2020 at 10:43

1 Answer 1

2

Yes it is possible but you should do it like this:

import os
output = os.popen("dir")
preprocessed = output.read()
print(preprocessed)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.