【问题标题】:Can python detect which OS is it running under?python可以检测到它在哪个操作系统下运行吗?
【发布时间】:2011-06-10 18:47:09
【问题描述】:

python 可以检测操作系统,然后为文件系统构造 if/else 语句。

我需要将 Fn 字符串中的 C:\CobaltRCX\ 替换为 FileSys 字符串。

import os.path, csv
from time import strftime

if os.path.?????:## Windows
   FileSys = r"C:\\working\\" 
else:   ##linux   
   FileSys = r"\\working\\" 

y=(strftime("%y%m%d"))
Fn = (r"C:\\working\\Setup%s.csv" %y)

【问题讨论】:

标签: python windows linux


【解决方案1】:

我通常只用这个:

import os
if os.name == 'nt':
    pass # Windows
else:
    pass # other (unix)

编辑:

希望能回应您的 cmets:

from time import strftime
import os

if os.name == 'nt': # Windows
    basePath = 'C:\\working\\'
else:
    basePath = '/working/'

Fn = '%sSetup%s.csv' % ( basePath, strftime( '%y%m%d' ) )

【讨论】:

  • 嗯?你这是什么意思?
  • Fn 字符串“C:\\working\\Setup%s.csv”。第二个变量如何。 1、filesys 2nd y的日期。现在我有日期设置,需要“filesys”字符串的帮助 yada yada yada “date”。谢谢
  • 不确定我现在是否用我的编辑来回答它,但我真的不明白。否则请尝试使用全面的英语进行解释...
  • 是的,当然,抱歉。如果您的路径变得比这更复杂,您还可以使用 os.path 连接多个路径序列。
【解决方案2】:

使用sys.platform。你可以在这里找到更多信息http://docs.python.org/library/platform.html

【讨论】:

    【解决方案3】:

    请看这里:https://stackoverflow.com/a/58689984/3752715

    import platform 
    plt = platform.system()
    
    if   plt == "Windows":   print("Your system is Windows")
    elif plt == "Linux":     print("Your system is Linux")
    elif plt == "Darwin":    print("Your system is MacOS")
    else:                    print("Unidentified system")
    

    你可以查看我的 github repo https://github.com/sk3pp3r/PyOS 并使用 pyos.py 脚本

    【讨论】:

      【解决方案4】:

      是的。

      >>> import os
      >>> os.uname()
      ('Linux', 'ubuntu', '2.6.32-27-generic', '#49-Ubuntu SMP Thu Dec 2 00:51:09 UTC 2010', 'x86_64')
      >>> system = os.uname()
      >>> print system[0] + '/' + system[1]
      Linux/ubuntu
      >>> 
      

      【讨论】:

        【解决方案5】:

        试试这个:

            import platform
            platform.uname()
        

        它可以在 linux 和 windows 上运行。仅供参考:os.uname() 不能在 Windows 上运行,但它可以在 linux 上运行。平台是通用的。

        【讨论】:

          【解决方案6】:

          迟到的答案,但如果你想确定路径分隔符,你可以使用

          os.path.sep
          

          【讨论】:

            【解决方案7】:

            你可以看看 os.uname

            In [12]: os.uname()
            Out[12]: 
            ('Darwin',
             'demitasse.local',
             '10.6.0',
             'Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386',
             'i386')
            

            【讨论】:

              【解决方案8】:

              对于大多数用例,您应该使用os.platform 模块。但是,如果您需要更精简的界面,请尝试platinfo

              【讨论】:

                【解决方案9】:

                这是我前几天刚刚创建的:

                代码:

                def GetUserPlatform():
                    if sys.platform == 'win32':
                        UsrWinVer = str(sys.getwindowsversion().major)
                        print("Operating System: Windows " + UsrWinVer)
                    else:
                        print("Something else")
                
                GetUserPlatform()
                

                输出:

                操作系统:Windows 10

                【讨论】:

                【解决方案10】:
                import platform
                print(platform.uname().system)
                

                这将为您提供 Windows、Linux 等。

                【讨论】:

                  猜你喜欢
                  • 2015-06-20
                  • 2020-07-21
                  • 2019-10-04
                  • 2018-03-03
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-02-21
                  • 2021-09-07
                  相关资源
                  最近更新 更多