Python: What OS am I running on?

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

    I know this is an old question but I believe that my answer is one that might be helpful to some people who are looking for an easy, simple to understand pythonic way to detect OS in their code. Tested on python3.7

    from sys import platform
    
    
    class UnsupportedPlatform(Exception):
        pass
    
    
    if "linux" in platform:
        print("linux")
    elif "darwin" in platform:
        print("mac")
    elif "win" in platform:
        print("windows")
    else:
        raise UnsupportedPlatform
    

提交回复
热议问题