Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid console window with .pyw file containing os.system call?

If I save my code files as .pyw, no console window appears - which is what I want - but if the code includes a call to os.system, I still get a pesky console window. I assume it's caused by the call to os.system. Is there a way to execute other files from within my .pyw script without raising the console window at all?

like image 260
twneale Avatar asked Nov 19 '09 17:11

twneale


1 Answers

The solution that Piotr describes is actually not as complicated as it may sound. Here is an example where a startupinfo is passed to a check_call invocation to suppress the console window:

startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

subprocess.check_call(cmd, startupinfo=startupinfo)

Since the convenience functions call, check_call, and check_output forward their **kwargs to the Popen constructor, it is not required to use Popen directly.

like image 82
Frank S. Thomas Avatar answered Oct 07 '22 17:10

Frank S. Thomas