0

Here are the list of task I would like to automate and capture the output into a text file using Python 3:

  1. Run the executable auth_bin
  2. Asks for entering password
  3. After entering the password from keyboard, it gives an output on the terminal
  4. Asks for choosing a number
  5. Generates a file on disk

I can automate inputing my password and the choice number with the following code, but cannot capture the output in step#3.

import subprocess from getpass import getpass choice_num = "5" mypass = getpass('Enter your password: ') subprocess.run(auth_binary, input="\n".join([mypass, choice_num]), text=True) 

How can I capture screen output into a text file?

BTW, I can give two inputs (step# 2 and 4) from the keyboard and save the screen output from my Mac terminal this way:

auth_bin 2>&1 | tee output.txt

1 Answer 1

0

To capture the stream of a process, you can use subprocess.Popen, and take advantage of the 'stdout' and 'stderr' arguments to capture the outputs and errors.

There's a good example here on how to save these to a file with Python 3.

Hope this solution suits your needs.

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

1 Comment

Thanks @A.Ber for your answer. I tried it, but it does not prompt me for the password. Seems like the password prompt goes into the captured file. I experimented with subprocess.run and took the output into a variable. I can serve my purpose but needs some extra cleanup.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.