Python运行Shell命令并模拟用户输入 [英] Python run shell command and emulate user input

查看:714
本文介绍了Python运行Shell命令并模拟用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从python脚本中模拟用户数据,例如:

How can i from a python script emulate user data like:

说我运行os.system('grunt init:gruntfile'),我想要python脚本会自动输入某些数据,因此python脚本会输入一些在脚本中进行硬编码的数据,而不是用户输入shell命令要求的数据?

Say i run os.system('grunt init:gruntfile'), and i want the python script automatically to 'enter' some data, so instead of the user inputs the data that the shell command asks for, the python script enters some data hard-coded in the script?

希望我的问题很有意义,有人可以提供帮助。
谢谢你,亚当

Hope my question makes sense and that someone can help. Thank you in advance, adam

推荐答案

pexpect 如果普通的子流程很可能适合您的账单没有。

pexpect will probably fit your bill if plain old subprocess doesn't.

pexpect 网站:


例如::

For example::

child = pexpect.spawn('scp foo myname@host.example.com:.')
child.expect ('Password:')
child.sendline (mypassword)


这篇关于Python运行Shell命令并模拟用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆