Python Popen - 如何使用python在嵌套子shell中执行命令 [英] Python Popen - how to execute commands in nested sub shell using python

查看:48
本文介绍了Python Popen - 如何使用python在嵌套子shell中执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 的嵌套 adb 子 shell 中执行命令时遇到问题.在 adb shell 中执行command_two"会在命令行中打开一个子控制台(并且控制台等待输入).我如何执行命令(输入控制台)在使用 python 的控制台中.

"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.

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

推荐答案

请试试这个:

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')

这篇关于Python Popen - 如何使用python在嵌套子shell中执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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