Python: What OS am I running on?

前端 未结 26 1567
野趣味
野趣味 2020-11-22 05:44

What do I need to look at to see whether I\'m on Windows or Unix, etc?

相关标签:
26条回答
  • 2020-11-22 06:20

    /usr/bin/python3.2

    def cls():
        from subprocess import call
        from platform import system
    
        os = system()
        if os == 'Linux':
            call('clear', shell = True)
        elif os == 'Windows':
            call('cls', shell = True)
    
    0 讨论(0)
  • 2020-11-22 06:20

    For Jython the only way to get os name I found is to check os.name Java property (tried with sys, os and platform modules for Jython 2.5.3 on WinXP):

    def get_os_platform():
        """return platform name, but for Jython it uses os.name Java property"""
        ver = sys.platform.lower()
        if ver.startswith('java'):
            import java.lang
            ver = java.lang.System.getProperty("os.name").lower()
        print('platform: %s' % (ver))
        return ver
    
    0 讨论(0)
  • 2020-11-22 06:20

    Check the available tests with module platform and print the answer out for your system:

    import platform
    
    print dir(platform)
    
    for x in dir(platform):
        if x[0].isalnum():
            try:
                result = getattr(platform, x)()
                print "platform."+x+": "+result
            except TypeError:
                continue
    
    0 讨论(0)
  • 2020-11-22 06:22

    Interesting results on windows 8:

    >>> import os
    >>> os.name
    'nt'
    >>> import platform
    >>> platform.system()
    'Windows'
    >>> platform.release()
    'post2008Server'
    

    Edit: That's a bug

    0 讨论(0)
  • 2020-11-22 06:25

    How about a new answer:

    import psutil
    psutil.MACOS   #True (OSX is deprecated)
    psutil.WINDOWS #False
    psutil.LINUX   #False 
    

    This would be the output if I was using MACOS

    0 讨论(0)
  • 2020-11-22 06:25

    I am using the WLST tool that comes with weblogic, and it doesn't implement the platform package.

    wls:/offline> import os
    wls:/offline> print os.name
    java 
    wls:/offline> import sys
    wls:/offline> print sys.platform
    'java1.5.0_11'
    

    Apart from patching the system javaos.py (issue with os.system() on windows 2003 with jdk1.5) (which I can't do, I have to use weblogic out of the box), this is what I use:

    def iswindows():
      os = java.lang.System.getProperty( "os.name" )
      return "win" in os.lower()
    
    0 讨论(0)
提交回复
热议问题