2012-11-20 4 views
3

QProcesscmd.exestdin을 통해 명령을 전달할 수없는 것으로 보입니다. 다른 커맨드 라인 앱을 사용해 보았습니다.QProcess가 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입니다 여기

몇 가지 간단한 내가 시도하는 데 사용하는 코드와 디버깅이다 "명령은 발행되지 않았다?

답변

0

출력을 읽기 전에 close the write channel이 필요합니다.

이 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' 
0

.

    start(...) 방법은 값을 반환하지 않습니다
  1. 인수가 (분명히)와 같이 빈 문자열을 전달
  2. 좋은 생각하지만, waitForStarted()readAllStandardOutput() 전화 waitForReadyRead()를 호출하기 전에
  3. 않습니다.

    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())) 
    
    :
  4. waitForFinished()이 당신의 예를 들어 라인을 따라 최소한의 작업 버전해야 당신이하지 않는 한 (또는 단순히 시간 아웃) 과정 (cmd.exe를)이 실제로

을 종료 반환하지 않습니다

관련 문제