【问题标题】:What is the multiplatform alternative to subprocess.getstatusoutput (older commands.setstatusoutput() from Python?什么是 subprocess.getstatusoutput(来自 Python 的旧 commands.setstatusoutput() 的多平台替代方案?
【发布时间】:2010-11-14 16:21:21
【问题描述】:

以下代码在 Python 3.0 中已过时,已被 subprocess.getstatusoutput() 替换。

import commands
(ret, out) = commands.getstatusoutput('some command')
print ret
print out

真正的问题是 Python 中这个命令的多平台替代方案是什么,因为上面的代码在 Windows 下确实失败了,因为 getstatusoutput 仅在 Unix 下受支持,而 Python 不会告诉你这一点,而是你会得到类似的东西:

>test.py
1
'{' is not recognized as an internal or external command,
operable program or batch file.

【问题讨论】:

    标签: python redirect process


    【解决方案1】:

    这将是 getstatusoutput() 的多平台实现:

    def getstatusoutput(cmd): 
        """Return (status, output) of executing cmd in a shell."""
        """This new implementation should work on all platforms."""
        import subprocess
        pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, universal_newlines=True)  
        output = "".join(pipe.stdout.readlines()) 
        sts = pipe.returncode
        if sts is None: sts = 0
        return sts, output
    

    【讨论】:

    • 感谢您的银弹。
    • 在 Windows Python 2.7 上为我工作——非常感谢这个代码大师
    【解决方案2】:

    我不会真正考虑这个多平台,但你可以使用subprocess.Popen:

    import subprocess
    pipe = subprocess.Popen('dir', stdout=subprocess.PIPE, shell=True, universal_newlines=True)
    output = pipe.stdout.readlines()
    sts = pipe.wait()
    print sts
    print output
    

    这是getstatusoutput 的直接替换:

    def getstatusoutput(cmd): 
        """Return (status, output) of executing cmd in a shell."""
        """This new implementation should work on all platforms."""
        import subprocess
        pipe = subprocess.Popen(cmd, shell=True, universal_newlines=True,
                stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output = str.join("", pipe.stdout.readlines()) 
        sts = pipe.wait()
        if sts is None:
            sts = 0
        return sts, output
    

    这个sn-p是由原发帖人提出的。自从getstatusoutputstderr 复制到stdout 之后,我做了一些更改。


    问题在于dir 并不是真正的多平台调用,但subprocess.Popen 允许您在任何平台上执行shell 命令。除非您绝对需要,否则我会避免使用 shell 命令。改为调查 osos.pathshutil 包的内容。

    import os
    import os.path
    for rel_name in os.listdir(os.curdir):
      abs_name = os.path.join(os.curdir, rel_name)
      if os.path.isdir(abs_name):
        print('DIR:  ' + rel_name)
      elif os.path.isfile(abs_name):
        print('FILE: ' + rel_name)
      else:
        print('UNK?  ' + rel_name)
    

    【讨论】:

    • 谢谢。我认为您没有使用社区 wiki 选项,因为我无法更新您的答案。我添加了另一个。
    • 它被标记为社区wiki,但我认为你的声誉必须超过100才能编辑;)
    • 哦,在这种情况下,您可以使用我在上一个答案中发布的代码更新您的解决方案,以便我可以接受回复并清理问题吗?谢谢。
    【解决方案3】:

    getstatusoutput 文档说它像这样运行命令:

    { cmd } 2>&1

    这显然不适用于 cmd.exe(如果需要,2>&1 可以正常工作)。

    您可以像上面一样使用 Popen,但也可以包含参数 'stderr=subprocess.STDOUT' 以获得与 getstatusoutput 相同的行为。

    我在 Windows 上的测试将返回码设置为 None,如果您指望返回值,这并不理想。

    【讨论】:

      猜你喜欢
      • 2021-05-13
      • 2014-10-05
      • 2019-03-31
      • 2023-01-19
      • 2011-04-07
      • 2022-06-10
      • 2021-11-13
      • 2018-12-01
      • 1970-01-01
      相关资源
      最近更新 更多