How to hide output of subprocess in Python 2.7

前端 未结 5 1178
无人及你
无人及你 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
    

提交回复
热议问题