【问题标题】:Run adb pipe commands in python在 python 中运行 adb 管道命令
【发布时间】:2023-04-10 13:27:02
【问题描述】:

我一直在尝试通过python脚本运行下面的adb命令:

'ls -1r | grep -m1 house | xargs tail -n2'

请注意,在此行之前,我已经导航到 adb shell 中的正确目录,并且我已经手动测试了它按预期工作的命令。在做了一些研究后,我做了以下尝试:

使用getoutput:

output = commands.getoutput('ls -1r | grep -m1 house | xargs tail -n2')

使用 Popen:

args0 = ['ls','-1r']
args1 = ['grep','-m1','house']
args2 = ['xargs','tail','-n2']
p0 = Popen(args0, shell=True, stdout=PIPE,stderr=PIPE)
p1 = Popen(args1, shell=True,stdin=p0.stdout, stdout=PIPE,stderr=PIPE)
p2 = Popen(args2, shell=True, stdin=p1.stdout,stdout=PIPE,stderr=PIPE)
(output,stderr) = p2.communicate()

但是输出/stderr 是NONE''。我也试过删除shell=True,或者把p0.stdout.close()放在通信()行之前,也没有做任何事情。

不知道为什么它不起作用,感谢任何帮助!

【问题讨论】:

    标签: python pipe adb popen


    【解决方案1】:

    您可以通过子进程运行 adb 命令。确保命令链有效

    subprocess.check_output(["adb", "shell", "ls -1r | grep -m1 house | xargs tail -n2"])
    

    【讨论】:

    • 我试过这个subprocess.check_output(["ls -1r | grep -m1 house | xargs tail -n2"]) 但它给我一个错误说 "OSError: [Errno 2] No such file or directory" 。我还需要获取输出而不是仅仅运行它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 2021-09-11
    • 2017-01-22
    • 2020-04-02
    相关资源
    最近更新 更多