SoFunction
Updated on 2025-05-14

Several ways to judge the current operating system in Python

This article introduces three methods for judging operating systems in Python. The following methods will be divided into these parts:

  • Python
  • Python
  • Python ()

Python

Python can use the method of judging the operating system. Here, taking Python 3 as an example, it will return the results of posix, nt, and java. Import os is required before use.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
print()

# The output on Ubuntu 16.04 is as follows:# posix

# The output on MacOS 10.15.7 is as follows:# posix

# The output on Windows 10 is as follows:# nt

There is another uname() function that can be used under the os module, which will return the operating system-related version information.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
print(())

# The output on Ubuntu 16.04 is as follows:# sysname='Linux', nodename='shengyu', release='4.10.0-40-generic', version='#44~16.04.1-Ubuntu SMP Thu Nov 9 15:37:44 UTC 2017', machine='x86_64'

# The output on MacOS 10.15.7 is as follows:# posix.uname_result(sysname='Darwin', nodename='', release='19.6.0', version='Darwin Kernel Version 19.6.0: Thu Sep 16 20:58:47 PDT 2021; root:xnu-6153.141.40.1~1/RELEASE_X86_64', machine='x86_64')

# No () under Windows

There is a more detailed classification, which will be introduced in the next section.

Python

The returned results are as follows:

  • AIX: 'aix'
  • Linux: 'linux'
  • Windows: 'win32'
  • Windows/Cygwin: 'cygwin'
  • macOS: 'darwin'

If you want to use judging the operating system, you can use startswith(). For example, the case of linux and linux2 can be included in a string starting with linux and written in the same conditional formula.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys

if ('linux'):
    print('Linux')
elif ('darwin'):
    print('macOS')
elif ('win32'):
    print('Windows')

Python ()

Python can use the () function to determine the operating system, which will return the name of the operating system, such as Linux, Darwin, Java, and Windows. If the operating system cannot be judged, an empty string will be returned.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import platform

print(())
print(())

# The output on Ubuntu 16.04 is as follows:# Linux
# 4.10.0-40-generic

# The output on MacOS 10.15.7 is as follows:# Darwin
# 19.6.0

# The output on Windows 10 is as follows:# Windows
# 10

Method supplement

Python determines whether the current system is Linux, Windows or MacOS

Using the sys module

import sys
 
if ("win"):
    print("The current system is Windows")
elif ("linux"):
    print("The current system is Linux")
elif ("darwin"):
    print("The current system is Mac OS")
else:
    print("The current system is another operating system")

The identifier of the current system platform will be returned. Linux is 'linux', Windows is 'win32', or Windows is 'win64', and macOS is 'darwin'. You can use the startswith() function to make judgments.

Using platform module

import platform
 
system = ()
if system == "Windows":
    print("The current system is Windows")
elif system == "Linux":
    print("The current system is Linux")
elif system == "Darwin":
    print("The current system is Mac OS")
else:
    print("The current system is another operating system")

Using the os module

import os

system = 
if system == "nt":
    print("The current system is Windows")
elif system == "posix":
    print("The current system is Linux or Mac OS")
else
    print("The current system is another operating system")

Python determines operating system type

Method 1

import platform

def TestPlatform():
    print ("----------Operation System--------------------------")
    #Windows will be : (32bit, WindowsPE)
    #Linux will be : (32bit, ELF)
    print(())

    #Windows will be : Windows-XP-5.1.2600-SP3 or Windows-post2008Server-6.1.7600
    #Linux will be : Linux-2.6.18-128.el5-i686-with-redhat-5.3-Final
    print(())

    #Windows will be : Windows
    #Linux will be : Linux
    print(())

    print ("--------------Python Version-------------------------")
    #Windows and Linux will be : 3.1.1 or 3.1.3
    print(platform.python_version())

def UsePlatform():
  sysstr = ()
  if(sysstr =="Windows"):
    print ("Call Windows tasks")
  elif(sysstr == "Linux"):
    print ("Call Linux tasks")
  else:
    print ("Other System tasks")

UsePlatform()

Method 2

import platform


def TestPlatform():
    return ()[0]


def mysubData(subData):
    b = []

    for i in subData:
        try:
            if (i):
                i = "Null"
                (i)
            if TestPlatform() == "64bit":
                if isinstance(i, numpy.float64):
                    (i)
                elif isinstance(i, numpy.int64):
                    (i)
            elif TestPlatform() == "32bit":
                if isinstance(i, numpy.float32):
                    (i)
                elif isinstance(i, numpy.int32):
                    (i)
        except:
            if isinstance(i, str):
                if "'" in i:
                    ("'", "")
                elif "%" in i:
                    ("'", "")
                elif "\\" in i:
                    ("'", "")
                (i)
            else:
                (i)

    return b

This is the end of this article about several methods for judging the current operating system in Python. For more related content on judging the operating system, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!