如何使用 Python 2.5 以友好的方式获取操作系统名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1421357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the operating system name in a friendly manner using Python 2.5?
提问by Nicolas Dumazet
I tried:
我试过:
print os.name
And the output I got was:
我得到的输出是:
:nt
However, I want output more like "Windows 98", or "Linux".
但是,我希望输出更像“Windows 98”或“Linux”。
After suggestions in this question, I also tried:
在这个问题的建议之后,我也尝试过:
import os
print os.name
import platform
print platform.system()
print platform.release()
And my output was:
我的输出是:
Traceback (most recent call last):
File "C:/Documents and Settings/BIU1LR/Desktop/python_programs/program/platform.py", line 3, in <module>
import platform
File "C:/Documents and Settings/BIU1LR/Desktop/python_programs/program\platform.py", line 4, in <module>
print platform.system()
AttributeError: 'module' object has no attribute 'system'
I am using Python 2.5.2. What am I doing wrong?
我正在使用 Python 2.5.2。我究竟做错了什么?
回答by Thomas Owens
Try:
尝试:
import platform
print platform.system(), platform.release()
I tried this on my computer with Python 2.6 and I got this as the output:
我用 Python 2.6 在我的电脑上试过这个,我得到了这个作为输出:
Windows XP
After your latest edits, I see that you called your script platform.py. This is causing a naming problem, as when you call platform.system()
and platform.release()
, it's looking in your file, and not Python's platform module. If you change the name of your file, all of your problems should be resolved.
在您最近的编辑之后,我看到您调用了您的脚本 platform.py。这会导致命名问题,因为当您调用platform.system()
and 时platform.release()
,它正在查看您的文件,而不是 Python 的平台模块。如果您更改文件的名称,您的所有问题都应该得到解决。
回答by Nicolas Dumazet
it is because you named your program "platform". Hence when importing the module "platform", your program is imported instead in a circular import.
这是因为您将程序命名为“平台”。因此,在导入模块“平台”时,您的程序会以循环导入的方式导入。
Try renaming the file to test_platform.py, and it will work.
尝试将文件重命名为 test_platform.py,它会起作用。
回答by Raghvendra
import platform
platform.dist()
回答by carlord
well it depends on the OS: for example I had tested
好吧,这取决于操作系统:例如我已经测试过
platform.system() - in linux works, AIX works
platform.release()- in linux works, AIX gives a weird '1' with non other info
platform.dist() - in linux works, AIX gives a nothing '','',''
os.name - resolves 'posix' in both :S
Windows I really don't test nor care :P
Windows 我真的不测试也不关心:P