[英]Cross platform code in python
How can I write in python some windows code to execute only when I am running the script in widnows, if I should run it in linux, that part of the windows code should be ignored, something simillar to this, in C++: 我怎么能在python中编写一些windows代码才能在widnows中运行脚本时执行,如果我应该在linux中运行它,那部分windows代码应该被忽略,这与C ++相似:
#ifdef windows
//code
#endif
#ifdef linux
//code
#endif
I tried something like this in python: 我在python中尝试过类似的东西:
if os.name = 'nt':
#code
But in linux it gives me an error(I am using STARTF_USESHOWWINDOW, witch gives error). 但在linux中它给了我一个错误(我使用STARTF_USESHOWWINDOW,女巫给出了错误)。
startupinfo = None
if sys.platform == 'win32':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW #error here
startupinfo.wShowWindow = _subprocess.SW_HIDE # error here
Traceback (most recent call last):
File "/home/astanciu/workspace/test/src/main.py", line 10, in <module>
import _subprocess
ImportError: No module named _subprocess
在Python中比在C中要少得多的地方检查平台是必要的。如果你真的必须这样做,首选的方法是检查sys.platform
而不是os.name
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.