How to hide output of subprocess in Python 2.7

前端 未结 5 1170
无人及你
无人及你 2020-11-22 01:14

I\'m using eSpeak on Ubuntu and have a Python 2.7 script that prints and speaks a message:

import subprocess
text = \'Hello World.\'
print text
subprocess.ca         


        
相关标签:
5条回答
  • 2020-11-22 01:41

    Here's a more portable version (just for fun, it is not necessary in your case):

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from subprocess import Popen, PIPE, STDOUT
    
    try:
        from subprocess import DEVNULL # py3k
    except ImportError:
        import os
        DEVNULL = open(os.devnull, 'wb')
    
    text = u"René Descartes"
    p = Popen(['espeak', '-b', '1'], stdin=PIPE, stdout=DEVNULL, stderr=STDOUT)
    p.communicate(text.encode('utf-8'))
    assert p.returncode == 0 # use appropriate for your program error handling here
    
    0 讨论(0)
  • 2020-11-22 01:46

    Use subprocess.check_output (new in python 2.7). It will suppress stdout and raise an exception if the command fails. (It actually returns the contents of stdout, so you can use that later in your program if you want.) Example:

    import subprocess
    try:
        subprocess.check_output(['espeak', text])
    except subprocess.CalledProcessError:
        # Do something
    

    You can also suppress stderr with:

        subprocess.check_output(["espeak", text], stderr=subprocess.STDOUT)
    

    For earlier than 2.7, use

    import os
    import subprocess
    with open(os.devnull, 'w')  as FNULL:
        try:
            subprocess._check_call(['espeak', text], stdout=FNULL)
        except subprocess.CalledProcessError:
            # Do something
    

    Here, you can suppress stderr with

            subprocess._check_call(['espeak', text], stdout=FNULL, stderr=FNULL)
    
    0 讨论(0)
  • 2020-11-22 01:49

    Why not use commands.getoutput() instead?

    import commands
    
    text = "Mario Balotelli" 
    output = 'espeak "%s"' % text
    print text
    a = commands.getoutput(output)
    
    0 讨论(0)
  • 2020-11-22 01:55

    As of Python3 you no longer need to open devnull and can call subprocess.DEVNULL.

    Your code would be updated as such:

    import subprocess
    text = 'Hello World.'
    print(text)
    subprocess.call(['espeak', text], stderr=subprocess.DEVNULL)
    
    0 讨论(0)
  • 2020-11-22 02:02

    Redirect the output to DEVNULL:

    import os
    import subprocess
    
    FNULL = open(os.devnull, 'w')
    retcode = subprocess.call(['echo', 'foo'], 
        stdout=FNULL, 
        stderr=subprocess.STDOUT)
    

    It is effectively the same as running this shell command:

    retcode = os.system("echo 'foo' &> /dev/null")
    

    Update: This answer applies to the original question relating to python 2.7. As of python >= 3.3 an official subprocess.DEVNULL symbol was added.

    retcode = subprocess.call(['echo', 'foo'], 
        stdout=subprocess.DEVNULL, 
        stderr=subprocess.STDOUT)
    
    0 讨论(0)
提交回复
热议问题