#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Location
    Telford UK
    Posts
    2
    Rep Power
    0

    Help with redirecting subprocess stdout


    Hi Folks this is my first ever Python Post in any forum...

    I have been busy learnin Pyhon in conjunction with PYQT4 and find it very usefull, however Ive come up against a brick wall...

    What I am trying to do is capture the terminal (stdout) output of a subprocess and display it in a QTextEdit box.

    The process I am interested in is an Arch Linux process called pacman.

    Normally you would type in a terminal: pacman -Syy to update the central package database. The process gives a streaming text output to the terminal.

    I want to capture this stream in a PyQt Gui and display it.

    Any help would be great...so far I have this code:

    ===============================================
    #!/usr/bin/python
    import sys
    import io
    from PyQt4 import QtGui, QtCore, uic

    class MyWindow(QtGui.QMainWindow):
    def __init__(self):
    super(MyWindow, self).__init__()
    uic.loadUi('getout.ui', self)
    self.show()


    def doit(self):
    oldstdout = sys.stdout
    sys.stdout = io.StringIO()


    print("Here is some text")
    #process = subprocess.Popen(['python','-h'])
    process = subprocess.Popen(['pacman','-Syy'])

    self.textEdit.setText(sys.stdout.getvalue() )
    sys.stdout = oldstdout


    if __name__ == '__main__':
    import sys
    import subprocess
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())

    ===============================================

    I get the output from the print command but the subprocess command output goes straight to the terminal....


    Please Help

    Thanks

    Steve
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    I'm fairly sure you're using python3.
    I'm also fairly sure you can use
    Code:
    with io.StringIO() as capture:
        print('whatever', file = capture)
    instead of that old trick of moving sys.stdout around. But anyway, that's not the problem. You need stdout of the subprocess. And you get that by reading the subprocess module over and over. And then some more.

    You might be able to use

    output = subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False, timeout=None)

    Otherwise, you might need

    subprocess.Popen(args, stdout=subprocess.PIPE)

    I'm sleepy. You can wade through it. Maybe the example half way down is useful:
    Code:
    with Popen(["ifconfig"], stdout=PIPE) as proc:
        log.write(proc.stdout.read())
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Location
    Telford UK
    Posts
    2
    Rep Power
    0
    Thank you very much I will try this....

    I developed another solution using an embeded Terminal (xterm)

    I created a frame QWidget in QT designer called "win" and embeded xterm thus:

    def doit(self):
    wid=self.win.winId()
    os.system("xterm -into %d -geometry 60x20 -sb -e 'python'&" % wid)

    This gives the output of the command 'python' to the embeded terminal which is so cool !!

    Will try your solution though as it will be neater to get the output in a Text Box.

    Thanks again
    Steve.

IMN logo majestic logo threadwatch logo seochat tools logo