
[英]Python Standalone Executable file does not run and shows error in Command Prompt
[英]How to run executable in Python 3 while passing two inputs to the prompt and redirecting output to a text file
以下是我想使用 Python 3 将 output 自动化并捕获到文本文件中的任务列表:
auth_bin
我可以使用以下代码自动输入我的密码和选择编号,但无法在步骤#3 中捕获 output。
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)
如何将屏幕 output 捕获到文本文件中?
顺便说一句,我可以从键盘输入两个输入(步骤#2和4)并以这种方式从我的Mac终端保存屏幕output:
auth_bin 2>&1 | tee output.txt
要捕获进程的 stream,您可以使用subprocess.Popen ,并利用“stdout”和“stderr”arguments 来捕获输出和错误。
这里有一个很好的例子,说明如何使用 Python 3 将这些保存到文件中。
希望此解决方案适合您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.