使用 subprocess.Popen 隐藏控制台 [英] Hide console with subprocess.Popen

查看:111
本文介绍了使用 subprocess.Popen 隐藏控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 subprocess.Popen 的新手并且一直在阅读文档.我只是想让我的过程不打开控制台.我需要这些参数中的哪些?

I'm new to subprocess.Popen and have been reading the docs. I'm just trying to make my process not open a console. Which of these parameters do I need?

stdout=subprocess.PIPE,
shell=False,
creationflags = CREATE_NO_WINDOW

这仅适用于 Windows 吗?我需要做一些不同的事情才能在 Mac 上运行吗?

Will this only work for Windows? Do I need to do something different for it to work on Mac?

谢谢!

推荐答案

stdout=subprocess.PIPE 对是否出现控制台没有影响.它只是确定子进程的 stdout 是否被捕获在您可以读取的管道中.

stdout=subprocess.PIPE has no effect on whether or not a console appears. It just determines whether or not the stdout of the subprocess is captured in a pipe that you can read from or not.

shell=False 是所有 subprocess 命令的默认值,因此您不需要提供它.它告诉 subprocess 是否使用 shell 来执行提供的命令.它对控制台是否出现在任何平台上没有任何影响.

shell=False is the default for all subprocess commands, so you don't need to provide it. It tells subprocess whether or not to use a shell to execute the provided command. It does not have any effect on whether or not a console appears on any platform.

creationflags = CREATE_NO_WINDOW 确实会在 Windows 上隐藏控制台(在 Windows 7 上测试,假设 CREATE_NO_WINDOW == 0x08000000).但是,在非 Windows 平台上使用它会导致错误.您应该参考这个问题,了解仅提供creationflags 在 Windows 上,以及隐藏控制台的替代方法.

creationflags = CREATE_NO_WINDOW will indeed hide the console on Windows (tested on Windows 7, assuming CREATE_NO_WINDOW == 0x08000000). It will cause an error to use it on non-Windows platforms, though. You should reference this question for a way to only provide creationflags on Windows, and also for alternative way to hide the console.

请注意,出现的控制台应该只是 Windows 上的问题.在 Posix 平台上,运行子进程时不应出现控制台.

Note that the console appearing should only be an issue on Windows. On Posix platforms the console shouldn't ever appear when running subprocesses.

这篇关于使用 subprocess.Popen 隐藏控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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