Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if the operating system a Python script is running on is Unix-like?

Tags:

python

unix

I'm trying to determine if the operating system is Unix-based from a Python script. I can think of two ways to do this but both of them have disadvantages:

  • Check if platform.system() is in a tuple such as ("Linux", "Darwin"). The problem with this is that I don't want to provide a list of every Unix-like system every made, in particular there are many *BSD varieties.
  • Check if the function os.fchmod exists, as this function is only available on Unix. This doesn't seem like a clean or "Pythonic" way to do it.
like image 767
user2722730 Avatar asked Aug 27 '13 18:08

user2722730


1 Answers

import sys
if 'win' in sys.platform():
    #windows
else:
    #not windows

or, you can try importing a platform dependent library

try:
    import windows_only as generic
except ImportException:
    try:
          import unix_only as generic
    except ImportException:
          import stdlib.module as generic

 print generic.common_function()

and then there's the always reliable

>>> import os
>>> os.name
nt
like image 57
blakev Avatar answered Sep 27 '22 23:09

blakev