Skip to content

OS Modules for Files and Dir

  • 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 a with block 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 os
import fnmatch
# 1. List all files specifically
with os.scandir('.') as entries:
for entry in entries:
if entry.is_file():
print(f"File: {entry.name}")
# 2. List all directories specifically
with 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}")

  • 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 folder
os.mkdir('example_directory')
# Create a deep, nested folder structure all at once
os.makedirs('2018/10/05')
# Traverse a directory tree recursively
for dirpath, dirnames, files in os.walk('.'):
print(f'Found directory: {dirpath}')
for file_name in files:
print(file_name)

  • 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 tempfile
import 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 destroyed
print('Exists outside block?', os.path.exists(tmpdir)) # Output: False

  • Concept: While os handles basic deletions, the shutil (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 os
import 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 contents
trash_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 copy
shutil.copy2(src, dst) # Copy preserving creation/modification times
shutil.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 rename

  • Concept: Compressing files/folders into archives, or extracting data from existing archives.
  • Methods (Manual control via zipfile): Use tarfile for TarFile
    • zipfile.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 os
import zipfile
# Manually writing a directory tree into a zip file
with 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 manually
with 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' file
shutil.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' formats
shutil.unpack_archive('backup.tar', 'extract_dir/')