2

I am trying to execute command lines in a pyqt application, here is what I am doing so far:

stdouterr = os.popen4(cmd)[1].read()

simple, and for the most part it does work, but when I open up a text file for example, the pyqt programs stops until the text file is closed. Is there a way I can have something like that open and not stop my application.

Edit:

Okay I almost figured it out. I am currently doing this:

Popen(cmd, shell=True,
         stdin=None, stdout=None, stderr=None, close_fds=True)

which does want I want it to, but is there a way to read stdout and stderr after the process is done running?

1
  • the os.popen family of functions is deprecated. Use the subprocess module instead. Commented Oct 5, 2013 at 17:28

2 Answers 2

2
#my pyqt knowledge is not the best but this works for me... didn't use your example. hope you get it still.
from PyQt5.QtCore import QProcess

process = QProcess()
process.start("yourcommand")
process.waitForStarted()
process.waitForFinished()
process.readAll()
process.close()

'''
from PyQt5.QtCore import QProcess
process = QProcess()
process.start('driverquery')
process.waitForStarted()
process.waitForFinished():
process.waitForReadyRead()
tasklist = process.readAll()
process.close()
tasklist = str(tasklist).strip().split("\\r\\n")
print(tasklist)
'''
Sign up to request clarification or add additional context in comments.

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
0

You can read from stdout and stderr like so:

process = subprocess.Popen(cmd, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)

stdout, stderr = process.communicate()
print stdout
print stderr

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.