Python QProcess无法写入cmd.exe
我似乎无法让Python QProcess无法写入cmd.exe,python,qt,cmd,pyside,qprocess,Python,Qt,Cmd,Pyside,Qprocess,我似乎无法让QProcess通过stdin将命令传递给cmd.exe。我也尝试过其他命令行应用程序 下面是一些我用来尝试和调试的简单代码: prog = "c:/windows/system32/cmd.exe" arg = [""] p = QtCore.QProcess() retval = p.start(prog, arg) print retval print p.environment() print p.error() p.waitForStarted() print("Start
QProcess
通过stdin
将命令传递给cmd.exe
。我也尝试过其他命令行应用程序
下面是一些我用来尝试和调试的简单代码:
prog = "c:/windows/system32/cmd.exe"
arg = [""]
p = QtCore.QProcess()
retval = p.start(prog, arg)
print retval
print p.environment()
print p.error()
p.waitForStarted()
print("Started")
p.write("dir \n")
time.sleep(2)
print(p.readAllStandardOutput())
print(p.readAllStandardError())
p.waitForFinished()
print("Finished")
print p.ExitStatus()
输出:
None
[]
PySide.QtCore.QProcess.ProcessError.UnknownError
Started
{时间流逝}
Finished
PySide.QtCore.QProcess.ExitStatus.NormalExit
QProcess: Destroyed while process is still running.
那么“dir\n
”命令是否从未发出过?在读取输出之前,您可能需要先发出该命令
这在WinXP上适用于我:
from PySide import QtCore
process = QtCore.QProcess()
process.start('cmd.exe')
if process.waitForStarted(1000):
# clear version message
process.waitForFinished(100)
process.readAllStandardOutput()
# send command
process.write('dir \n')
process.closeWriteChannel()
process.waitForFinished(100)
# read and print output
print process.readAllStandardOutput()
else:
print 'Could not start process'
您的代码有几个问题
start(…)
方法不返回值,但waitForStarted()
返回值readAllStandardOutput()
之前,请调用waitForReadyRead()
waitForFinished()
将不会返回(或仅超时),除非您使进程(cmd.exe)实际退出from PySide import QtCore
prog = "cmd.exe"
arg = []
p = QtCore.QProcess()
p.start(prog, arg)
print(p.waitForStarted())
p.write("dir \n")
p.waitForReadyRead()
print(p.readAllStandardOutput())
p.write("exit\n")
p.waitForFinished()
print("Finished: " + str(p.ExitStatus()))