Cross-platform subprocess with hidden window

endolith picture endolith · Jun 19, 2009 · Viewed 17k times · Source

I want to open a process in the background and interact with it, but this process should be invisible in both Linux and Windows. In Windows you have to do some stuff with STARTUPINFO, while this isn't valid in Linux:

ValueError: startupinfo is only supported on Windows platforms

Is there a simpler way than creating a separate Popen command for each OS?

if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    proc = subprocess.Popen(command, startupinfo=startupinfo)
if os.name == 'posix':
    proc = subprocess.Popen(command)    

Answer

Anurag Uniyal picture Anurag Uniyal · Jun 19, 2009

You can reduce one line :)

startupinfo = None
if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(command, startupinfo=startupinfo)