I am running a Python program on Windows with:
- no
explorer.exerunning, no desktop, no start menu, typical for embedded computers. If you want to try, you can emulate this by killingexplorer.exein the Task manager ; note that I do it differently but out of topic here pythonw.exeinstead ofpython.exeto avoid a to have acmd.exeterminal window
If you run pythonw test.py with
import os
os.system("shutdown /r /t 3600")
you will see a terminal window flashing during < 1 second.
Same problem with subprocess.call(...).
Same problem with subprocess.run(...) like in Is there a command in Python for instant shutdown on Windows 10?.
(Note: if explorer.exe is killed, you can call the Task Manager with CTRL+SHIFT+ESCAPE and Run command to launch pythonw test.py)
Question: How to call Windows tools like shutdown with 1) no explorer.exe 2) pythonw.exe, without seeing a cmd.exe window flashing?
The solution is:
Context (pasted from @Mofi's comments):
os.systemis declared as deprecated since more than 10 years and should not be used anymore at all. There is thesubprocessmodule which gives a Python script writer the full control on Windows how an executable is run by calling the Windows kernel library functionCreateProcesswithout or with aSTARTUPINFOstructure.os.system()calls the system function which on Windows results in callingCreateProcesswith%ComSpec%(defined with%SystemRoot%\System32\cmd.exeexpanding usually toC:\Windows\System32\cmd.exe) with the option/c(run command line and close) and the command line passed as argument toos.systemappended as additional arguments forcmd.exe.In this case
cmd.exesearches for a file with nameshutdownin the current directory and next in the directories as listed in local environment variablePATHwith no file extension at all or with a file extension as listed in local environment variablePATHEXT. It hopefully finds in this case after several file system accesses%SystemRoot%\System32\shutdown.exeand calls nowCreateProcessto run this batch file with using the standard streams ofcmd.exeand waiting for self-termination ofshutdown.exeafter whichcmd.execloses itself.There can be used
os.environto get the string value of the environment variableSystemRoot. This directory path string can next be concatenated with the string"\\System32\\shutdown.exe"to get the fully qualified file name of the executable to run. It is very unlikely that the environment variableSystemRootis not defined at all. That is possible only if a user explicitly deletes this environment variable in a command prompt window before starting in same command prompt window Python for interpreting a Python script.There should be used the string
"C:\\Windows"in case ofenviron["SystemRoot"]returns no string (or an empty string). However, it is advisable to check the existence of finally determined fully qualified file name of the Windows command SHUTDOWN and inform the user if the executable is not existing instead of a blind execution of this executable and hoping it really exists although this is most likely always the case (at least now but who knows the future). Thensubprocess.Popen()can be used to runshutdown.exewith its full path.subprocess.Popen()offerssubprocess.CREATE_NO_WINDOWfor callingCreateProcesswith the Process Creation FlagCREATE_NO_WINDOWset indwCreationFlagsfor not opening a new console window for the Windows console applicationshutdown.exe.