OS Modules for Files and Dir
Directory Listing & Pattern Matching
Section titled “Directory Listing & Pattern Matching”- Scanning directories to list contents, differentiate between files and folders, and filter by specific naming patterns.
- Methods:
os.scandir(path): Returns an iterator of directory entries. It is highly efficient because it caches file attributes (like checking if it is a file or folder). Use it inside awithblock to ensure it closes properly.entry.is_file()/entry.is_dir(): Boolean checks on the scanned entries.fnmatch.fnmatch(filename, pattern): Checks if a string matches a specific Unix-style wildcard pattern.
Program:
import osimport fnmatch
# 1. List all files specificallywith os.scandir('.') as entries: for entry in entries: if entry.is_file(): print(f"File: {entry.name}")
# 2. List all directories specificallywith os.scandir('.') as entries: for entry in entries: if entry.is_dir(): print(f"Directory: {entry.name}")
# 3. Filter by pattern (e.g., find all text backups)for filename in os.listdir('.'): if fnmatch.fnmatch(filename, 'data_*_backup.txt'): print(f"Matched: {filename}")Directory Creation & Traversal
Section titled “Directory Creation & Traversal”- Concept: Building folder structures and walking through complex, deeply nested directory trees.
- Methods:
os.mkdir(path): Creates a single directory. Fails if the parent directory doesn’t exist.os.makedirs(path): Recursively creates a deep directory structure (creates all necessary intermediate parent folders).os.walk(path): Generates the file names in a directory tree by walking either top-down or bottom-up. It yields a 3-tuple:(dirpath, dirnames, filenames).
import os
# Create a single folderos.mkdir('example_directory')
# Create a deep, nested folder structure all at onceos.makedirs('2018/10/05')
# Traverse a directory tree recursivelyfor dirpath, dirnames, files in os.walk('.'): print(f'Found directory: {dirpath}') for file_name in files: print(file_name)Temporary Directories
Section titled “Temporary Directories”- Concept: Creating secure, temporary workspaces that automatically clean themselves up (and delete all contents) the moment you are done with them.
- Methods:
tempfile.TemporaryDirectory(): Acts as a context manager. Yields the absolute path to the temporary folder.
Program:
import tempfileimport os
with tempfile.TemporaryDirectory() as tmpdir: print('Created temporary directory:', tmpdir) print('Exists inside block?', os.path.exists(tmpdir)) # Output: True # Do your processing, temporary file writing, etc. here
# The moment the 'with' block ends, the directory and everything inside is destroyedprint('Exists outside block?', os.path.exists(tmpdir)) # Output: FalseDeleting, Copying, and Moving (shutil)
Section titled “Deleting, Copying, and Moving (shutil)”- Concept: While
oshandles basic deletions, theshutil(shell utilities) module is required for high-level operations like recursive deletions, copying files with metadata, and moving structures. - Methods:
os.remove(path)/os.rmdir(path): Deletes a single file or an empty directory.shutil.rmtree(path): Deletes an entire directory tree, regardless of whether it contains files.shutil.copy(src, dst): Copies file data and permissions.shutil.copy2(src, dst): Copies file data, permissions, and preserves all metadata (timestamps).shutil.copytree(src, dst): Recursively copies an entire directory tree.shutil.move(src, dst): Moves a file or directory. If the destination exists as a folder, it moves it inside. If not, it effectively renames it.
import osimport shutil
# --- Deleting ---data_file = 'test.txt'if os.path.isfile(data_file): os.remove(data_file)else: os.rmdir(data_file) # Only works if directory is completely empty
# Force delete a directory and all its contentstrash_dir = 'my_documents/bad_dir'try: shutil.rmtree(trash_dir)except OSError as e: print(f'Error deleting {trash_dir}: {e.strerror}')
# --- Copying ---src = 'path/to/file.txt'dst = 'path/to/dest_dir'
shutil.copy(src, dst) # Standard copyshutil.copy2(src, dst) # Copy preserving creation/modification timesshutil.copytree('data_1', 'data1_backup') # Duplicate an entire folder
# --- Moving and Renaming ---shutil.move('dir_1/', 'backup/') # Move inside backup/ (or rename to backup/ if missing)os.rename('first.zip', 'first_01.zip') # Standard renameArchiving (Zipping and Extracting)
Section titled “Archiving (Zipping and Extracting)”- Concept: Compressing files/folders into archives, or extracting data from existing archives.
- Methods (Manual control via
zipfile): Usetarfilefor TarFilezipfile.ZipFile(file, mode): Opens a zip archive for reading ('r') or writing ('w').zf.write(path): Adds a file to the archive.zf.extractall(path): Extracts everything to a target directory.- Methods (Automated control via
shutil- Best Practice): shutil.make_archive(base_name, format, root_dir): Instantly zips an entire directory in one line.shutil.unpack_archive(filename, extract_dir): Instantly extracts an archive.
Program (Manual Control):
import osimport zipfile
# Manually writing a directory tree into a zip filewith zipfile.ZipFile("file.zip", "w") as zf: for dirpath, dirnames, files in os.walk("any_directory"): zf.write(dirpath) for filename in files: # os.path.join creates the correct path format for your OS (Windows \\ vs Linux /) zf.write(os.path.join(dirpath, filename))
# Extracting manuallywith zipfile.ZipFile('file.zip', 'r') as zf: zf.extractall(path='extract_dir')Program (Easiest Automated Way):
import shutil
# Instantly compress the 'data/' folder into a 'backup.zip' fileshutil.make_archive('data/backup', 'zip', 'data/')
# Instantly extract 'backup.tar' into the 'extract_dir/' folder# Note: Python natively supports 'zip', 'tar', 'gztar', 'bztar', and 'xztar' formatsshutil.unpack_archive('backup.tar', 'extract_dir/')