Fundamentals 22 min read

Unlock Python’s sys and os Modules: Essential Functions and Practical Examples

This article introduces Python’s sys and os modules, detailing their most useful functions for runtime configuration, file and path manipulation, process control, and environment handling, and provides clear code examples—including a simple progress bar and a demonstration of sys.argv versus __file__ behavior—to help readers master essential system programming techniques.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Unlock Python’s sys and os Modules: Essential Functions and Practical Examples

Introduction

For Python learners, the os and sys modules are essential for interacting with the runtime environment and the operating system.

sys Module Overview

The sys module provides a set of practical services, including many functions and variables for handling Python runtime configuration, resources, and system interaction.

# command‑line arguments list, first element is the script path
sys.argv
# return imported modules dictionary
sys.modules
# exit program, e.g., sys.exit(0)
sys.exit(n)
# return module search path list
sys.path
# write to standard output
sys.stdout.write('output')
# read a line from standard input
sys.stdin.readline()[:-1]
# list of imported module names
sys.modules.keys()
# list of imported module objects
sys.modules.values()
# exception info
sys.exc_info()
# version info (hex)
sys.hexversion
# version string
sys.version
# C API version
sys.api_version
# version info tuple
sys.version_info
# display hook
sys.displayhook(value)
# exception hook
sys.excepthook(type, value, traceback)
# default encoding
sys.getdefaultencoding()
# filesystem encoding for Unicode filenames
sys.getfilesystemencoding()
# set default encoding (rarely used)
sys.setdefaultencoding(name)
# builtin module names
sys.builtin_module_names
# executable path
sys.executable
# Windows version info
sys.getwindowsversion()
# copyright string
sys.copyright
# byte order
sys.byteorder
# clear current thread error
sys.exc_clear()
# execution prefix
sys.exec_prefix
# standard error
sys.stderr
# standard input
sys.stdin
# standard output
sys.stdout
# platform name
sys.platform
# max Unicode value
sys.maxunicode
# max int value (Python 2 only)
sys.maxint
# call tracing
sys.call_tracing(func, args)
# clear type cache
sys._clear_type_cache()
# current frames dict
sys._current_frames()
# DLL handle
sys.dllhandle
# bytecode generation flag
sys.dont_write_bytecode
# check interval
sys.getcheckinterval()
# dlopen flags (Unix)
sys.getdlopenflags()
# reference count
sys.getrefcount(object)
# size of object in bytes
sys.getsizeof(object, default=None)
# get current frame
sys._getframe(depth=0)
# get profile function
sys.getprofile()
# get trace function
sys.gettrace()
# Windows version named tuple
sys.getwindowsversion()

A simple progress‑bar example prints a "#" every second using sys.stdout.write and sys.stdout.flush:

import sys, time
for i in range(10):
    sys.stdout.write('#')
    sys.stdout.flush()
    time.sleep(1)

Two files illustrate the difference between sys.argv[0] and __file__:

# cg.py
import sys

def ff():
    print(sys.path[0], ':', sys.argv[0], ':', __file__)

ff()

# fd.py
import cg
cg.ff()

Running fd.py shows that sys.argv[0] refers to the main script ( fd.py) while __file__ reports the imported module’s filename ( cg.py).

Use dir() to list all attributes of a module and help() for detailed usage.

os Module Overview

The os module offers a comprehensive suite for file and directory operations, path manipulation, process management, environment handling, and accessing file statistics.

File Operations

# close file descriptor
os.close(hw)
# close a range of descriptors
os.closerange(hw1, hw2)
# change working directory via descriptor
os.fchdir(hw)
# change file mode
os.fchmod(hw, mode)
# change file ownership
os.fchown(hw, uid, gid)
# force write to disk
os.fdatasync(hw)
# open file object from descriptor
os.fdopen(hw, mode='r')
# get file system config
os.fpathconf(hw, name)
# get file descriptor status
os.fstat(hw)
# get file system stats for descriptor
os.fstatvfs(hw)
# sync descriptor to disk
os.fsync(hw)
# truncate file via descriptor
os.ftruncate(hw, length)
# duplicate descriptor
os.dup(hw)
# duplicate descriptor to specific number
os.dup2(hw, hw1)
# get terminal process group
os.tcgetpgrp(hw)
# set terminal process group
os.tcsetpgrp(hw, hw1)
# temporary filename
os.tempnam([dir], [prefix])
# temporary file object (auto‑deleted)
os.tmpfile()
# temporary pathname
os.tmpnam()
# get terminal device name
os.ttyname(hw)
# delete a pathname
os.unlink(path)
# update access and modification times
os.utime(path, times)
# walk directory tree
os.walk(top, topdown=True, onerror=None, followlinks=False)
# write to descriptor
os.write(hw, str)
# get filesystem stats
os.statvfs(path)
# get current working directory
os.getcwd()
# get Unicode cwd (Python 2)
os.getcwdu()
# check if descriptor is a tty
os.isatty(hw)
# set file flags (Unix)
os.lchflags(path, flags)
# change permissions of a symlink
os.lchmod(path, mode)
# change ownership of a symlink
os.lchown(path, uid, gid)
# list directory contents
os.listdir(path)
# reposition file descriptor offset
os.lseek(fd, pos, how)
# open file with flags
os.open(file, flags, mode=0o777)
# open pseudo‑terminal
os.openpty()
# get configuration info for path
os.pathconf(path, name)
# path separator
os.pathsep
# parent directory name
os.pardir
# create pipe
os.pipe()
# open pipe to command
os.popen(command, mode='r', bufsize=-1)
# platform name
os.name
# read from descriptor
os.read(hw, n)
# walk (alias)
os.walk(path)
# listdir alias
os.listdir(path)
# change working directory
os.chdir(path)
# rename file
os.rename(oldfile, newfile)

Path Operations

# split path and filename
os.path.split(filename)
# split extension
os.path.splitext(filename)
# directory name
os.path.dirname(filename)
# base name
os.path.basename(filename)
# join paths
os.path.join(dirname, basename)
# absolute path
os.path.abspath(name)
# split UNC path
os.path.splitunc(path)
# normalize path
os.path.normpath(path)
# check existence
os.path.exists(path)
# check if absolute
os.path.isabs(path)
# real path
os.path.realpath(path)
# relative path
os.path.relpath(path, start=None)
# normalize case
os.path.normcase(path)
# is directory
os.path.isdir(path)
# is file
os.path.isfile(path)
# is symbolic link
os.path.islink(path)
# is mount point
os.path.ismount(path)
# same file
os.path.samefile(path1, path2)
# access time
os.path.getatime(path)
# modification time
os.path.getmtime(path)
# creation time
os.path.getctime(path)
# file size
os.path.getsize(path)
# common prefix of paths
os.path.commonprefix(list)
# lexical existence (including broken links)
os.path.lexists(path)
# expand user '~'
os.path.expanduser(path)
# expand environment variables
os.path.expandvars(path)
# same open file
os.path.sameopenfile(fp1, fp2)
# same stat result
os.path.samestat(stat1, stat2)
# split drive (Windows)
os.path.splitdrive(path)
# walk with callback (deprecated)
os.path.walk(path, visit, arg)
# unicode filename support
os.path.supports_unicode_filenames()

Process Management

# create symbolic link
os.symlink(src, dst)
# create hard link
os.link(src, dst)
# lstat (no follow links)
os.lstat(path)
# execute shell command (output shown)
os.system('bash command')
# change root directory of process
os.chroot(path)
# popen and read output
output = os.popen('command').read()
# line separator for platform
os.linesep
# path separator
os.sep
# major device number
os.major(device)
# make device number
os.makedev(major, minor)
# make directories recursively
os.makedirs(path, mode=0o777)
# minor device number
os.minor(device)
# make directory
os.mkdir(path, mode=0o777)
# make FIFO (named pipe)
os.mkfifo(path, mode=0o666)
# create special file node
os.mknod(filename, mode=0o600, device=0)

Environment Parameters

# check access mode
os.access(path, mode)
# set file flags
os.chflags(path, flags)
# change mode
os.chmod(path, mode)
# change owner
os.chown(path, uid, gid)
# get login name
os.getlogin()
# CPU count
os.cpu_count()
# generate random bytes
os.urandom(n)
# environment mapping
os.environ
# get specific variable
os.getenv('CYGWIN')
# set default env variable
os.environ.setdefault(key, value)
# get env variable as bytes
os.getenvb('CYGWIN')

stat Submodule

# obtain file stats
fileStats = os.stat(path)
# mode
fileStats[stat.ST_MODE]
# size
fileStats[stat.ST_SIZE]
# modification time
fileStats[stat.ST_MTIME]
# access time
fileStats[stat.ST_ATIME]
# creation time
fileStats[stat.ST_CTIME]
# is directory
stat.S_ISDIR(fileStats[stat.ST_MODE])
# is regular file
stat.S_ISREG(fileStats[stat.ST_MODE])
# is symbolic link
stat.S_ISLNK(fileStats[stat.ST_MODE])
# is socket
stat.S_ISSOCK(fileStats[stat.ST_MODE])
# is FIFO
stat.S_ISFIFO(fileStats[stat.ST_MODE])
# is block device
stat.S_ISBLK(fileStats[stat.ST_MODE])
# is character device
stat.S_ISCHR(fileStats[stat.ST_MODE])

One‑Click Crash Example

Demonstrates opening Notepad repeatedly using os.system and os.popen:

# infinite loop opening Notepad
while True:
    os.system('notepad')

Using popen to capture output:

pp = os.popen('notepad')
print(pp.read())

These snippets illustrate how the os module can launch external programs and interact with their output.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonprocess managementOS modulesys moduleFile OperationsEnvironment Variablespath manipulation
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

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.