Python Popen - how to execute commands in nested s

2019-09-16 04:08发布

问题:

"I have an issue executing commands in nested adb sub shell in python. executing "command_two" in adb shell opens a sub console in command line (and the console waits for input). how do i execute commands (give input to the console) in that console using the python.

Code:

     R = subprocess.Popen('adb shell', stdin=subprocess.PIPE)
     R.communicate('command_one\ncommand_two\n)

回答1:

Please try this:

import time

R = subprocess.Popen('adb shell', shell=True, stdin=subprocess.PIPE)
R.communicate('command_one\n')
time.sleep(2)
R.communicate('command_two\n')