更新时间:2023-11-17 23:17:58
winpexpect
或 SendKeys
模块.请参阅我对具有相应代码示例的类似问题的回答
winpexpect
or SendKeys
modules. See my answer to a similar quesiton that has corresponding code examples以下是如何使用参数启动子进程、传递一些输入以及将合并的子进程的 stdout/stderr 写入文件的示例:
Here's an example how to start a subprocess with arguments, pass some input, and write merged subprocess' stdout/stderr to a file:
#!/usr/bin/env python3
import os
from subprocess import Popen, PIPE, STDOUT
command = r'fileLoc\uploader.exe -i file.txt -d outputFolder'# use str on Windows
input_bytes = os.linesep.join(["username@email.com", "password"]).encode("ascii")
with open('command_output.txt', 'wb') as outfile:
with Popen(command, stdin=PIPE, stdout=outfile, stderr=STDOUT) as p:
p.communicate(input_bytes)