简体   繁体   English

subprocess.Popen不运行bash命令吗?

[英]subprocess.Popen doesn't run the bash command?

I have tried using subprocess .Popen in Python IDLE, in both versions 2.7 and 3.2 and the code didn't work!!! 我已经尝试在2.7和3.2版本的Python IDLE中使用子过程.Popen,并且代码不起作用!!!

Python代码

Thank you. 谢谢。

I suspect that IDLE redirects Python's stdout so that print ing happens in the window, but it doesn't redirect file descriptor 1, so that any subprocess happily prints to that file descriptor. 我怀疑IDLE重定向Python的stdout以便在窗口中进行print ,但它不会重定向文件描述符1,因此任何子进程都会愉快地打印到该文件描述符。

Try to start IDLE from the command line, run these lines and see if their output is printed to the terminal where IDLE is called from. 尝试从命令行启动IDLE,运行这些行,看看它们的输出是否打印到调用IDLE的终端上。

Solutions: 解决方案:

  1. Don't use IDLE. 不要使用IDLE。 Call your stuff from a python interpreter called from the command line. 从命令行调用的python解释器中调用您的东西。
  2. Redirect the output of your processes to a pipe, read this pipe and output it. 将过程的输出重定向到管道,读取该管道并输出。 Such as: 如:

     sp = subprocess.Popen(['ls'], stdout=subprocess.PIPE) for i in sp.stdout: print i sp.wait() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM