Python: What OS am I running on?

前端 未结 26 1568
野趣味
野趣味 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:10

    Dang -- lbrandy beat me to the punch, but that doesn't mean I can't provide you with the system results for Vista!

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

    ...and I can’t believe no one’s posted one for Windows 10 yet:

    >>> import os
    >>> os.name
    'nt'
    >>> import platform
    >>> platform.system()
    'Windows'
    >>> platform.release()
    '10'
    
    0 讨论(0)
  • 2020-11-22 06:11

    Watch out if you're on Windows with Cygwin where os.name is posix.

    >>> import os, platform
    >>> print os.name
    posix
    >>> print platform.system()
    CYGWIN_NT-6.3-WOW
    
    0 讨论(0)
  • 2020-11-22 06:11

    I am late to the game but, just in case anybody needs it, this a function I use to make adjustments on my code so it runs on Windows, Linux and MacOs:

    import sys
    def get_os(osoptions={'linux':'linux','Windows':'win','macos':'darwin'}):
        '''
        get OS to allow code specifics
        '''   
        opsys = [k for k in osoptions.keys() if sys.platform.lower().find(osoptions[k].lower()) != -1]
        try:
            return opsys[0]
        except:
            return 'unknown_OS'
    
    0 讨论(0)
  • 2020-11-22 06:13

    Use platform.system()

    Returns the system/OS name, such as 'Linux', 'Darwin', 'Java', 'Windows'. An empty string is returned if the value cannot be determined.

    import platform
    system = platform.system().lower()
    
    is_windows = system == 'windows'
    is_linux = system == 'linux'
    is_mac = system == 'darwin'
    
    0 讨论(0)
  • 2020-11-22 06:15
    >>> import os
    >>> os.name
    'posix'
    >>> import platform
    >>> platform.system()
    'Linux'
    >>> platform.release()
    '2.6.22-15-generic'
    

    The output of platform.system() is as follows:

    • Linux: Linux
    • Mac: Darwin
    • Windows: Windows

    See: platform — Access to underlying platform’s identifying data

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

    try this:

    import os
    
    os.uname()
    

    and you can make it :

    info=os.uname()
    info[0]
    info[1]
    
    0 讨论(0)
提交回复
热议问题