子进程没有捕获标准输出 [英] subprocess not catching stdout

查看:44
本文介绍了子进程没有捕获标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几天以来一直试图弄清楚为什么我的 subprocess.Popen 没有捕获标准输出.也许这与子进程输出文件的事实有关.当我运行一个没有输出文件的子进程并且标准输出(你会在终端看到)被咳嗽时,我发现了这一点.你能帮我如何在带有输出文件的场景中捕获它吗,所以我不想从文件中捕获输出,而只想捕获终端输出,所以运行输出.非常感谢任何形式的帮助.

im trying since days to figure out why my subprocess.Popen does not catch the stdout. Maybe it has something to do with the fact that the subprocess outputs a file. I found this out when I run a subprocess without an output file and the stdout (that you would see at the terminal) was cought. Can you please help me how to catch it in the scenario with the output file, so I dont want to catch the output from the file but only the terminal output, so running output. Any kind of help is very appreciated.

     command = "cnvkit.py call %s -o %s" % (file, output)
     process_call = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=sfs.path('cnv_app/call_CNS/'), universal_newlines=True)
stdout, stderr = process_call.communicate()
     if process_call.returncode and stderr:
          output_call = stdout
          output_err_call = stderr
     else:
          output_call = stdout 
          process_call.poll()
          response = {'output_call': output_call, 'output_err_call': output_err_call}

推荐答案

我的子进程正在返回一个警告,该警告显然转到 stderr 并且因为我的 process_call.returncode 将返回 0,对于if 语句,只捕获了空的标准输出,这就是为什么我没有得到输出.我有点失望,当出现警告时,标准输出全部进入标准错误.是的,你是对的,你需要一个水晶球来回答这个问题,但是你帮我解决了我的问题 :)

My child process was returning a warning which apparently goes to stderr and since my process_call.returncode would return 0, which is False for the if statement, only the stdout was captured which was empty, and thats why i did not get the output. I am a little dissapointed that the stdout goes all to stderr when a warning appears. Yes you are right, you needed a crystal ball to answer the question, but hey you helped me to solve my Problem :)

这篇关于子进程没有捕获标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆