Search code examples
pythonoperating-systemcross-platformpython-2.7detection

Python 2.7, OS Detection return values


I am working on cross platform utility which involves drive scanning and the automagical creation of shell/batch files for the OSs upon which it runs. Unfortunately I cannot find one simple answer. As stated in these links:

  1. When to use os.name, sys.platform, or platform.system?
  2. Reliably detect Windows in Python
  3. Extract file name from path, no matter what the os/path format
  4. Python: What OS am I running on?
  5. and at http://docs.python.org/library/platform.html#module-platform

The various platform.system(), platform.platform(), sys.platform, os.name() etc. etc. all suffer the problem of not necessarily being future perfect. That is if an OS developer changes things a little, these may not work (at least until patched or revised). So obviously the best solution is to try a small part of each of the above, along with targeting some OS specific executable file with a call().

Which leaves my question: Since the best way to determine this involves platform.system, sys.platform, and os.name (assuming only generalized recognition is needed), what are the various possible outputs for those programs? The docs.python.org sections on each of these modules only lists a few, and the pages are not exactly current. Specifically I would like to know the possible output on the last three mac OS's, Win XP- Win 8, and just knowing Linux covers my needs there. Any one know what the outputs are or where i can find them?

Thanks in advance.

Clarification: What I am looking for here is the currently known values so that I can incorporate them into an existing project, with an eye towards future code revision being made easier on my end. So the CURRENT return values are what I am seeking (Last 3 gens of Mac OS* and Win * since beyond that probably isn't much used any more)


Solution

  • Edit: For the specific question of all possible return values:

    Note:

    As others have indicated, sys.platform is derived from the name that the system vendor gives their system.

    /Edit.


    Not sure it's possible to have something so future-perfect. If you only want to know the OS (mac/win/linux) then see the examples for sys.platform.

    Also, keeping things in a separate function like get_os_name lets you control or map the input-output if you see a lot of changes in the future - once a year per OS? Also convenient to combine with the other functions you've mentioned. So you can return a tuple based on (os_name, 32/64bit, variant) (where variant is things like XP, Win8, Darwin, etc.) depending on how it affects your script.

    The docs.python.org sections on each of these modules only lists a few, and the pages are not exactly current.

    Unfortunately true. But again, it's impossible to account for environments or platforms in the future or even all current ones. The logical thing to do is make sure it works on the current platforms that you have tested/developed for.