Using Python
You can gather a lot of information about the platform where Python is running by using the standard library platform
.
platform.system()
Returns the system/OS name, such as ‘Linux’, ‘Darwin’, ‘Java’, ‘Windows’. An empty string is returned if the value cannot be determined.
Darwin refers to macOS.
Using the click
module
click
integrates a useful function to find application folders.
Very often, you want to open a configuration file that belongs to your application. However, different operating systems store these configuration files in different locations depending on their standards. Click provides a get_app_dir() function which returns the most appropriate location for per-user config files for your application depending on the OS.
Example usage:
import os import click import ConfigParser APP_NAME = 'My Application' def read_config(): cfg = os.path.join(click.get_app_dir(APP_NAME), 'config.ini') parser = ConfigParser.RawConfigParser() parser.read([cfg]) rv = {} for section in parser.sections(): for key, value in parser.items(section): rv[f"{section}.{key}"] = value return rv
source: https://click.palletsprojects.com/en/8.1.x/utils/#finding-application-folders
Using shell commands
To determine the operating system you’re on, try the following commands:
Unix-based Systems (Linux, macOS, etc.)
This command provides information about the system, including the operating system name and version.
Linux Systems
This command displays detailed information about the distribution and version.
Windows Systems
This command displays the Windows version you’re running.
Distinguishing Linux and macOS
This command attempts to run lsb_release
(common on Linux) and SW_vers
(used on macOS) to display information about the OS.