我想在后台打开一个进程并与之交互,但是该进程在Linux和Windows中均不可见。在Windows中,您必须对STARTUPINFO做一些事情,而在Linux中这是无效的:
有比为每个操作系统创建单独的Popen命令更简单的方法吗?
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)
最佳答案
您可以减少一行:)
startupinfo = None
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(command, startupinfo=startupinfo)