Python运行多个提示命令终端

问题描述 投票:0回答:1

我想从python脚本运行终端命令。我知道我可以使用os.system()电话。但问题是当我运行第一个命令时,我得到一个提示,我必须写下一个终端命令。例如:-

./distance vectors_bow.bin 
Enter word or sentence (EXIT to break): EXIT

我试图使用os.system('./distance vectors_bow.bin & EXIT'),但我得到输出sh: 1: EXIT: not found

当我在终端中手动执行上述过程而不是从python脚本执行上述过程时,它工作正常。怎么做?

python shell subprocess system
1个回答
2
投票

如果我理解正确你想用参数distance运行vectors_bow.bin并有第一个输入EXIT

试试这个:

from subprocess import Popen, PIPE
Popen(['distance', 'vectors_bow.bin'], stdin=PIPE).communicate('EXIT'.encode())

编辑:修复了python3需要编码的输入参数

© www.soinside.com 2019 - 2024. All rights reserved.