我想在后台打开一个进程并与之交互,但是该进程在Linux和Windows中均不可见。在Windows中,您必须使用STARTUPINFO做一些事情,而在Linux中这是无效的:
ValueError:仅在Windows平台上支持startupinfo
有比为每个操作系统创建单独的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)