Python 2.7中subprocess模块没有DEVNULL的处理方法

我在使用Python 2.7过程中,想利用内置模块subprocess来多线程执行shell command。

希望将stderr输出直接不保存。

上网查资料,说可以用stderr = subprocess.DEVNULL来处理:

subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)

但提示subprocess模块并没有DEVNULL属性。

通过查文档发现,Python 2.7的subprocess模块是没有DEVNULL的,但Python 3.8有:

@ubuntu:~$ pydoc3.8 subprocess
NAME
    subprocess - Subprocesses with accessible I/O streams

MODULE REFERENCE
    https://docs.python.org/3.8/library/subprocess
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    This module allows you to spawn processes, connect to their
    input/output/error pipes, and obtain their return codes.
    
    For a complete description of this module see the Python documentation.
    
    Main API
    ========
    run(...): Runs a command, waits for it to complete, then returns a
              CompletedProcess instance.
    Popen(...): A class for flexibly executing a command in a new process
    
    Constants
    ---------
    DEVNULL: Special value that indicates that os.devnull should be used
    PIPE:    Special value that indicates a pipe should be created
    STDOUT:  Special value that indicates that stderr should go to stdout
    
    
    Older API
    =========
    call(...): Runs a command, waits for it to complete, then returns
        the return code.
    check_call(...): Same as call() but raises CalledProcessError()
        if return code is not 0
    check_output(...): Same as check_call() but returns the contents of
...

Python 2.7的文档是这样的:

@ubuntu:~$ pydoc2.7 subprocess

Help on module subprocess:

NAME
    subprocess - Subprocesses with accessible I/O streams

FILE
    /usr/lib/python2.7/subprocess.py

MODULE DOCS
    https://docs.python.org/library/subprocess

DESCRIPTION
    This module allows you to spawn processes, connect to their
    input/output/error pipes, and obtain their return codes.
    
    For a complete description of this module see the Python documentation.
    
    Main API
    ========
    call(...): Runs a command, waits for it to complete, then returns
        the return code.
    check_call(...): Same as call() but raises CalledProcessError()
        if return code is not 0
    check_output(...): Same as check_call() but returns the contents of
        stdout instead of a return code
    Popen(...): A class for flexibly executing a command in a new process
    
    Constants
    ---------
    PIPE:    Special value that indicates a pipe should be created
    STDOUT:  Special value that indicates that stderr should go to stdout

CLASSES
    __builtin__.object
        Popen
...

所以如果是用Python2.7,只能手动打开/dev/null了。

devnull = open("/dev/null", "w")
subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=devnull)
devnull.close()

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值