Search code examples
pythonpython-2.7operating-system

How do I check what platform (OS) I'm running in Python 2.7?


Is there any way to check what platform I'm running with Python 2.7? For example using the platform module:

import platform
print platform.system()
print platform.release()

I get:

Linux
2.6.32-312-ec2

But if I read from the file /etc/issue by running a Linux command line command within Python I can get exactly what platform I'm running:

import command
print commands.getoutput('cat /etc/issue')

.

 Debian GNU/Linux 6.0 \n \l

Is there any other way in Python to know that I'm running Debian GNU Linux 6.0?


Solution

  • I prefer sys.platform to get the platform. sys.platform will distinguish between linux, other unixes, and OS X while os.name is more general.

    These are done by:

    import sys
    print(sys.platform)
    
    import os
    print(os.name)
    

    For much more detailed information, use the platform module. This has cross-platform functions that will give you information on the machine architecture, OS and OS version, version of Python, etc. Also it has os-specific functions to get things like the particular linux distribution.

    A small example, which actually seems the best way to do what you want:

    import platform
    print(platform.platform())