Fundamentals 4 min read

Essential Python Standard Library Modules for System and File Operations

This article introduces several essential Python standard‑library modules—including os, shutil, subprocess, signal, sys, platform, logging, getpass, pwd, and grp—explaining their primary functions for file and process management, system information, logging, and user/group queries, and provides concise example code for each.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Essential Python Standard Library Modules for System and File Operations

os provides functions for interacting with the operating system, such as reading and writing files, handling directories, and accessing environment variables.

import os
print(os.listdir('.'))  # List all files and directories in the specified directory

shutil offers high‑level file operations including copying, moving, deleting, and handling archive files.

import shutil
shutil.copy('source.txt', 'destination.txt')  # Copy file

subprocess enables the creation of new processes and connection to their input, output, and error streams, allowing execution of external commands or scripts.

import subprocess
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
print(result.stdout)  # Print command output

signal allows programs to receive and handle Unix signals, which is useful for long‑running services.

import signal
def handler(signum, frame):
    print("收到信号", signum)
signal.signal(signal.SIGINT, handler)  # Set SIGINT signal handler

sys gives access to variables and functions closely related to the Python interpreter, such as exiting the program or retrieving command‑line arguments.

import sys
sys.exit()  # Exit the current program

platform retrieves underlying platform information, including hardware architecture and operating system name/version.

import platform
print(platform.system())  # Print operating system name

logging provides a flexible logging system that is important for system administration and debugging.

import logging
logging.basicConfig(level=logging.INFO)
logging.info("这是一个日志信息")

getpass is used to obtain passwords without echoing them to the screen and also supports retrieving the current username.

import getpass
print(getpass.getuser())  # Get current username

pwd (Unix) accesses the Unix password database, providing user account information.

import pwd
print(pwd.getpwuid(os.getuid()))  # Get current user's account information

grp (Unix) is similar to pwd but accesses the group database.

import grp
print(grp.getgrgid(os.getgid()))  # Get information about the current user's group
Pythonprocess managementfile managementos modulestandard-library
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

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.