繁体   English   中英

在shell上提示时如何制作python脚本类型

[英]How to make python script type when prompted on shell

与此类似的内容: How to make python script press 'enter' 当在 Shell 上出现提示时

就像那个问题,如果我有test_enter.py

print("press enter...")
input()
print("yay!")

和这个文件:

from subprocess import Popen, PIPE
p = Popen(['python test_enter.py'], stdin=PIPE, shell=True)
p.communicate(input=b'\n')

但不仅仅是一个新行,我希望它输入一些东西然后按“输入”。 那可能吗?

调用脚本:

print("write something here")
inserted_string = input()
print("You wrote: " + inserted_string)

调用脚本:

from subprocess import Popen, PIPE
import sys

print("custom insert here...")
inserted1 = input()

if len(sys.argv) > 1:
  inserted2 = sys.argv[1]
else:
  inserted2 = 'nothing here'

p1 = Popen(['python script_to_call.py'], stdin=PIPE, shell=True)
p1.communicate(input=b'standard string\n')

print(" --- ")

p2 = Popen(['python script_to_call.py'], stdin=PIPE, shell=True)
p2.communicate(input=inserted1.encode('utf-8') + b'\n')

print(" --- ")

p3 = Popen(['python script_to_call.py'], stdin=PIPE, shell=True)
p3.communicate(input=inserted2.encode('utf-8') + b'\n')

运行命令

python caller.py "string got from parameter"

输出:

custom insert here...
string got from input

write something here
You wrote: standard string
 --- 
write something here
You wrote: string got from input
 --- 
write something here
You wrote: string got from parameter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM