Python run shell command and emulate user input

拈花ヽ惹草 提交于 2019-12-08 08:12:42

问题


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

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


回答1:


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

From the pexpect site:

For example::

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


来源:https://stackoverflow.com/questions/10372012/python-run-shell-command-and-emulate-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!