Python Popen在Linux上工作,但在Windows上没有

发布于 2025-02-03 16:14:56 字数 740 浏览 1 评论 0原文

我想在Linux和Windows上运行相同的代码。下面的代码在Linux上工作,但在Windows上不起作用!

#!/usr/bin/env python3
import subprocess

def runcmd(cmd,show=True):
    print("cmd:" + cmd)
    try:
        p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        retval = p.communicate()[0]
        print(retval)
        return retval.decode('utf-8')
    except:
        print("cmd error:" + cmd)
        pass
    return ""

def main():
    runcmd("adb shell 'find /data/ -type f ! -empty'")
    return

if __name__ == "__main__":
    main()

Windows10的输出:

cmd:adb shell 'find /data/ -type f ! -empty'
b'/system/bin/sh: find /data/ -type f ! -empty: inaccessible or not found\r\n'

I would like to run the same code on linux and windows. below code works on linux but not on windows!

#!/usr/bin/env python3
import subprocess

def runcmd(cmd,show=True):
    print("cmd:" + cmd)
    try:
        p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        retval = p.communicate()[0]
        print(retval)
        return retval.decode('utf-8')
    except:
        print("cmd error:" + cmd)
        pass
    return ""

def main():
    runcmd("adb shell 'find /data/ -type f ! -empty'")
    return

if __name__ == "__main__":
    main()

The output from windows10:

cmd:adb shell 'find /data/ -type f ! -empty'
b'/system/bin/sh: find /data/ -type f ! -empty: inaccessible or not found\r\n'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡莣 2025-02-10 16:14:56

不要将整个ADB Shell命令放入一个参数中。使用:

runcmd("adb shell find /data/ -type f ! -empty")

您可能在Linux和Windows上具有不同版本的adb文档提到在Android平台中处理的参数在Android Platform中的处理方式发生了变化-Tools 23。

Don't put the entire adb shell command in a single argument. Use:

runcmd("adb shell find /data/ -type f ! -empty")

You may have different versions of adb on Linux and Windows. The documentation mentions that the way the arguments are processed changed in Android Platform-Tools 23.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文