The Python Oracle

How do I check the operating system in Python?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Isolated

--

Chapters
00:00 How Do I Check The Operating System In Python?
00:26 Answer 1 Score 5
00:35 Answer 2 Score 16
01:02 Accepted Answer Score 542
01:22 Answer 4 Score 7
01:30 Thank you

--

Full question
https://stackoverflow.com/questions/8220...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #linux #operatingsystem

#avk47



ACCEPTED ANSWER

Score 542


You can use sys.platform:

from sys import platform
if platform == "linux" or platform == "linux2":
    # linux
elif platform == "darwin":
    # OS X
elif platform == "win32":
    # Windows...

sys.platform has finer granularity than sys.name.

For the valid values, consult the documentation.

See also the answer to “What OS am I running on?”




ANSWER 2

Score 16


You can get a pretty coarse idea of the OS you're using by checking sys.platform.

Once you have that information you can use it to determine if calling something like os.uname() is appropriate to gather more specific information. You could also use something like Python System Information on unix-like OSes, or pywin32 for Windows.

There's also psutil if you want to do more in-depth inspection without wanting to care about the OS.




ANSWER 3

Score 7


More detailed information are available in the platform module.




ANSWER 4

Score 5


You can use sys.platform.