2

我正在尝试在 pyqt 应用程序中执行命令行,这是我目前正在做的事情:

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

很简单,而且在大多数情况下它确实有效,但是当我打开一个文本文件时,pyqt 程序会停止,直到文本文件关闭。有没有办法让我打开类似的东西而不停止我的应用程序。

编辑:

好吧,我几乎想通了。我目前正在这样做:

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

哪个确实想要我想要它,但是有没有办法在进程完成运行后读取 stdout 和 stderr ?

4

2 回答 2

1
#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)
'''
于 2018-03-21T08:03:58.927 回答
0

你可以像stdout这样阅读stderr

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
于 2013-10-06T06:48:37.463 回答