Fundamentals 5 min read

Master Python’s os Module: Essential File & Directory Operations

This guide walks you through Python’s os module, showing how to create, inspect, rename, and delete files and directories, manipulate paths, work with environment variables, execute system commands, and traverse directory trees using os.walk, all illustrated with clear examples and diagrams.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Master Python’s os Module: Essential File & Directory Operations
import os
os.mkdir(name)

– creates a new directory named

name

.

os.path.exists(name)

– checks whether a file or directory exists.

os.path.isdir(name)

– returns

True

if the specified path is a directory, otherwise

False

.

os.mknod(name)

– creates an empty file.

os.path.isfile(name)

– returns

True

if the path points to a regular file.

os.rmdir(name)

– removes an empty directory.

os.remove(name)

– deletes a file.

os.getcwd()

– returns the current working directory (equivalent to the Unix

pwd

command).

os.path.abspath(file|dir)

– obtains the absolute path of a file or directory.

os.path.basename(name)

– extracts the final component (file or folder name) from a path.

os.path.dirname(name)

– returns the directory portion of a path.

os.path.splitext

– splits a filename into name and extension.

os.path.split

– separates a path into directory and filename.

os.path.join(path, name)

– joins directory and filename into a single path.

os.chdir(dir)

– changes the current working directory.

os.listdir(dir)

– lists all files and sub‑directories in the specified directory.

os.path.getsize(file|dir)

– returns the size of a file in bytes; for a directory it returns

0

.

os.stat(name)

– retrieves file status information (permissions, timestamps, etc.).

os.system(command)

– executes a system command.

os.rename(old, new)

– renames a file or directory.

os.getuid()

– returns the current process’s user ID.

os.getgid()

– returns the current process’s group ID.

os.environ['VAR_NAME'] = 'value'

– sets an environment variable.

os.environ['VAR_NAME']

– retrieves the value of an environment variable.

os.getenv('VAR_NAME')

– another way to get an environment variable’s value.

os.walk(top, topdown=True, onerror=None, followlinks=False)

– generates the file‑system tree by walking the directory hierarchy either top‑down or bottom‑up.

top – the root directory from which the walk starts; each iteration yields a 3‑tuple

(dirpath, dirnames, filenames)

.

topdown – if

True

(default) the walk proceeds from the top down; if

False

it proceeds from the bottom up.

onerror – a callable that handles errors; it can either log the error and continue or raise an exception to abort the walk.

followlinks – if

True

, symbolic links to directories are traversed.

Backendpythonfile systemos moduledirectory operations
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.