【问题标题】:Receiving back string of lenght 0 from os.popen('cmd').read()从 os.popen('cmd').read() 接收长度为 0 的字符串
【发布时间】:2018-07-21 17:09:41
【问题描述】:

我正在使用一个名为“ideviceinfo”的命令行工具(请参阅https://github.com/libimobiledevice)来帮助我快速从我每天使用的 iOS 设备中获取串行、IMEI 和电池健康信息。它的执行速度比 Apple 自己的“cfgutil”工具快得多。

据了解,我已经能够开发一个比 PyCharm(我的主 IDE)中显示的脚本更复杂的脚本,以将特定值等分配给各个变量,然后使用 pyclip 和 pyautogui 之类的东西来帮助自动粘贴这些到我们使用的数据库应用程序的字段中。我还能够在 Mac OS X 终端和 python shell 中使用脚本的简化版本,而不会出现任何问题。

我希望使用 AppleScript 来帮助尽可能轻松地运行脚本。

当我尝试使用 Applescript 的“do shell script 'python script.py'”时,当我调用 'ideviceinfo' 时,我只会返回一个长度为零的字符串。当我尝试使用“python script.py”的“运行 Shell 脚本”组件构建 Automator 应用程序时,也会发生完全相同的事情。

我已尽力将问题隔离出来。当在脚本中调用其他更基本的命令(例如“日期”)时,它们会返回有效字符串。

    #!/usr/bin/python
    import os

    ideviceinfoOutput = os.popen('ideviceinfo').read()
    print ideviceinfoOutput
    print len (ideviceinfoOutput)

    boringExample = os.popen('date').read()
    print boringExample
    print len (boringExample)

我正在运行 Mac OS X 10.11 并使用 Python 2.7

谢谢。

【问题讨论】:

    标签: python-2.7 automator


    【解决方案1】:

    我想我已经设法自己解决了。我只需要更明确地说明“ideviceinfo”二进制文件(我希望这是正确的术语)在计算机上的存储位置。

    将一行代码改为

        ideviceinfoOutput = os.popen('/usr/local/bin/ideviceinfo').read()
    

    一切似乎又好了。

    【讨论】:

      猜你喜欢
      • 2015-04-27
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多