无法运行子进程,也无法将输出重定向到日志文件

2024-04-20 06:04:29 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码不会写入我正在创建的日志文件(Dm_日志.txt). 但如果我把stdout注释掉,stderr=过程.沟通()然后它会。如果我不使用communicate,那么子进程将暂停,因为我正在使用communicate进程。等等(). 如何解决?我需要写在日志文件和运行的过程太。你知道吗

  logfilePath = self.psexeclogs + 'Dm_Log.txt'
  logfile = file(logfilePath,'w')

  try:

   process = subprocess.Popen(drivemaster_open_command, stdout = subprocess.PIPE,stderr = subprocess.PIPE)
   stdout, stderr = process.communicate()

   for line in process.stderr:
    print ' '
    sys.stderr.write(line)
    logfile.write(line)
   process.wait()

  except OSError:
    print "********COULD NOT FIND PSEXEC.EXE, PLEASE REINSTALL AND SET THE PATH VARIABLE PROPERLY********\n"

  #Close the logfile first
  logfile.close()

Tags: 文件txt进程过程stderrstdoutlinedm
1条回答
网友
1楼 · 发布于 2024-04-20 06:04:29

在使用communicate等待进程终止后,就不能使用process.stderr,只能使用stderr。你知道吗

Popen.communicate(input=None)

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate

for line in stderr:
   print ' '
   sys.stderr.write(line)
   logfile.write(line)

相关问题 更多 >