Os.name

Overview:

  • Developing a portable program means writing a program that can be made to run on any operating system with minimal to no changes made to that program.
  • The os module implements functions that interact with the operating system directly.
  • Like any other modules of Python, the os module is also written adhering to the good principles of software design. 
  • The os module internally imports one of the required platform specific modules like posix or nt by checking the sys.builtin_module_names. If the sys.builtin_module_names contains "posix", the module posix is imported by the os module. Else, if the sys.builtin_module_names contains "nt" the module nt is imported.
  • The string literal os.name, reflects this name of the operating system dependent module being imported for portability by the os module.

Python os.name

Example:

# Example Python program that gets the current os dependent module
# loaded by the module os(which is written addressing portability considerations)
import os
import sys

# Modules this Python version was built with
print("The modules that were built into this Python interpreter:");
print(sys.builtin_module_names);

# Name of the module internally loaded by the os module
osDepModuleName = os.name;
print("os module has loaded the following os dependent module:");
print(osDepModuleName);

Output:

The modules that were built into this Python interpreter:

('_abc', '_ast', '_codecs', '_collections', '_functools', '_imp', '_io', '_locale', '_operator', '_peg_parser', '_signal', '_sre', '_stat', '_string', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', 'atexit', 'builtins', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'posix', 'pwd', 'sys', 'time', 'xxsubtype')

os module has loaded the following os dependent module:

posix

 


Copyright 2023 © pythontic.com