Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

os

Python’s os module provides portable interfaces to operating-system functionality, including environment variables, process parameters, files, directories, and file descriptors. Use os.path for pathname manipulation and specialized modules for higher-level file operations.

How it works

The os module exposes portable interfaces to operating-system facilities. Use it for process environment access, current-directory operations, file-descriptor work, and other system interactions; use os.path for pathname manipulation and higher-level modules such as shutil or tempfile for specialized tasks.

Syntax

import os
os.system(command)
os.stat(path, *, dir_fd=None, follow_symlinks=True)

Parameters

NameTypeDescription
commandstrCommand string to execute in a subshell.
pathpath-like object or file descriptorPath or file descriptor whose status is requested by os.stat().
dir_fdint or NoneOptional directory file descriptor for relative path resolution in functions that support it.
follow_symlinksboolWhether symbolic links are followed by functions that provide this option.

Returns

Return values depend on the selected os function; os.system() returns the command’s status, while os.stat() returns file-status information.

Key points

  • os is the standard-library gateway to operating-system features while presenting common interfaces where possible.
  • Python normally represents filenames, command-line arguments, and environment variables as strings.
  • os.system(command) runs the supplied command string through a subshell.
  • os.stat() obtains status information from either a path or a file descriptor.
  • Use os.path for higher-level pathname operations instead of assembling path logic manually.

Common mistakes

  • Text is the normal representation for names, arguments, and environment variables, but some operating-system interfaces also require bytes.
  • Do not use os as the default abstraction for every filesystem task; choose open() for file I/O and os.path for pathname operations.

Example: Inspect the process environment

import os

print(os.getcwd())
os.environ["CARD_EXAMPLE"] = "ready"
print(os.environ["CARD_EXAMPLE"])

Shows the current working directory and reads a value from the process environment.

When to use it

Read or update environment variables

A script needs configuration supplied by its execution environment.

Work with process directories

A backend task must inspect or change its working-directory context.

Inspect files and descriptors

A tool needs operating-system file metadata or lower-level descriptors.

Run a system command

A script must invoke a host command through the shell.

Interview questions

QWhat is the purpose of the os module?

The os module provides a portable interface to operating-system-dependent functionality. It covers areas such as environment variables, process parameters, files and directories, and file descriptors.

Follow-up: Which module should handle pathname manipulation? Use os.path; higher-level file and directory operations are handled by shutil.

QHow do you access environment variables with os?

The process environment is exposed through os.environ, which maps environment-variable names to values. The documentation describes both keys and values as strings.

Follow-up: Why might bytes matter? Some systems require names or values to be encoded or decoded before passing them to the operating system.

QWhat does os.system() do?

os.system(command) executes a command string in a subshell. It is therefore an interface to the host shell rather than a direct Python implementation of the command.

Follow-up: What should be considered before using it? Shell behavior and operating-system differences apply, so a more specialized subprocess interface may be preferable when precise process control is needed.

  • os.path
  • open()
  • fileinput
  • tempfile
  • shutil
  • os.environ
  • os.stat()
  • os.system()

Source: os — Miscellaneous operating system interfaces. Checked 2026-10-15.