问题
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