🎯 empfohlene Sammlungen
Balanced sample collections from various categories for you to explore
Web Python Dateioperationsbeispiele
Web Python Dateioperationsbeispiele einschließlich Textdatei-Lesen/Schreiben, Datei-Kopieren/Verschieben, Verzeichnisdurchlauf und Dateivalidierung
💻 Textdatei-Lesen/Schreiben python
🟢 simple
⭐⭐
Textdateien mit verschiedenen Kodierungsoptionen mit Python file I/O lesen und schreiben
⏱️ 20 min
🏷️ python, web, file operations
Prerequisites:
Basic Python, File I/O
# Web Python Text File Read/Write Examples
# Using Python file I/O for browser-based file operations (Pyodide)
# 1. Reading Text Files
def read_text_file(file_path):
"""
Read a text file and return its content
Args:
file_path: Path to the text file
Returns:
str: Content of the file
"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
return content
except FileNotFoundError:
print(f"Error: File '{file_path}' not found")
return None
except IOError as e:
print(f"Error reading file: {e}")
return None
def read_text_file_with_encoding(file_path, encoding='utf-8'):
"""
Read a text file with specific encoding
Args:
file_path: Path to the text file
encoding: Character encoding (default: utf-8)
Returns:
str: Content of the file
"""
try:
with open(file_path, 'r', encoding=encoding) as file:
content = file.read()
return content
except UnicodeDecodeError:
print(f"Error: Cannot decode file with {encoding} encoding")
return None
def read_text_file_line_by_line(file_path):
"""
Read a text file line by line
Args:
file_path: Path to the text file
Returns:
list: List of lines
"""
lines = []
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
lines.append(line.rstrip('\n'))
return lines
except FileNotFoundError:
print(f"Error: File '{file_path}' not found")
return None
def read_text_file_in_chunks(file_path, chunk_size=1024):
"""
Read a text file in chunks (useful for large files)
Args:
file_path: Path to the text file
chunk_size: Size of each chunk in bytes
Yields:
str: Chunks of text
"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
while True:
chunk = file.read(chunk_size)
if not chunk:
break
yield chunk
except FileNotFoundError:
print(f"Error: File '{file_path}' not found")
return
# 2. Writing Text Files
def write_text_file(file_path, content):
"""
Write text content to a file
Args:
file_path: Path to the text file
content: Text content to write
Returns:
bool: True if successful
"""
try:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
return True
except IOError as e:
print(f"Error writing file: {e}")
return False
def write_text_lines(file_path, lines):
"""
Write a list of lines to a text file
Args:
file_path: Path to the text file
lines: List of strings to write
Returns:
bool: True if successful
"""
try:
with open(file_path, 'w', encoding='utf-8') as file:
for line in lines:
file.write(line + '\n')
return True
except IOError as e:
print(f"Error writing file: {e}")
return False
def write_text_file_with_encoding(file_path, content, encoding='utf-8'):
"""
Write text content to a file with specific encoding
Args:
file_path: Path to the text file
content: Text content to write
encoding: Character encoding
Returns:
bool: True if successful
"""
try:
with open(file_path, 'w', encoding=encoding) as file:
file.write(content)
return True
except IOError as e:
print(f"Error writing file: {e}")
return False
def append_to_text_file(file_path, content):
"""
Append text content to an existing file
Args:
file_path: Path to the text file
content: Text content to append
Returns:
bool: True if successful
"""
try:
with open(file_path, 'a', encoding='utf-8') as file:
file.write(content)
return True
except IOError as e:
print(f"Error appending to file: {e}")
return False
# 3. File Info Helper
def get_file_size(file_path):
"""
Get the size of a file in bytes
Args:
file_path: Path to the file
Returns:
int: File size in bytes
"""
import os
try:
return os.path.getsize(file_path)
except FileNotFoundError:
print(f"Error: File '{file_path}' not found")
return None
def get_file_extension(file_path):
"""
Get the file extension
Args:
file_path: Path to the file
Returns:
str: File extension
"""
import os
return os.path.splitext(file_path)[1]
def get_file_name_without_extension(file_path):
"""
Get the file name without extension
Args:
file_path: Path to the file
Returns:
str: File name without extension
"""
import os
return os.path.splitext(os.path.basename(file_path))[0]
def format_file_size(bytes_size):
"""
Format file size in human-readable format
Args:
bytes_size: Size in bytes
Returns:
str: Formatted size string
"""
for unit in ['B', 'KB', 'MB', 'GB']:
if bytes_size < 1024.0:
return f"{bytes_size:.2f} {unit}"
bytes_size /= 1024.0
return f"{bytes_size:.2f} TB"
def get_file_details(file_path):
"""
Get detailed information about a file
Args:
file_path: Path to the file
Returns:
dict: File details
"""
import os
from datetime import datetime
try:
stat_info = os.stat(file_path)
return {
'name': os.path.basename(file_path),
'size': stat_info.st_size,
'readable_size': format_file_size(stat_info.st_size),
'extension': get_file_extension(file_path),
'modified_time': datetime.fromtimestamp(stat_info.st_mtime),
'created_time': datetime.fromtimestamp(stat_info.st_ctime),
'is_file': os.path.isfile(file_path),
'is_dir': os.path.isdir(file_path)
}
except FileNotFoundError:
print(f"Error: File '{file_path}' not found")
return None
# 4. File Validation
def file_exists(file_path):
"""
Check if a file exists
Args:
file_path: Path to the file
Returns:
bool: True if file exists
"""
import os
return os.path.exists(file_path)
def is_file(file_path):
"""
Check if path is a file
Args:
file_path: Path to check
Returns:
bool: True if it's a file
"""
import os
return os.path.isfile(file_path)
def is_directory(dir_path):
"""
Check if path is a directory
Args:
dir_path: Path to check
Returns:
bool: True if it's a directory
"""
import os
return os.path.isdir(dir_path)
def validate_file(file_path, max_size_mb=None):
"""
Validate file properties
Args:
file_path: Path to the file
max_size_mb: Maximum file size in MB
Returns:
dict: Validation result
"""
import os
result = {'valid': True, 'errors': []}
if not os.path.exists(file_path):
result['valid'] = False
result['errors'].append('File does not exist')
return result
if not os.path.isfile(file_path):
result['valid'] = False
result['errors'].append('Path is not a file')
return result
if max_size_mb:
size_mb = os.path.getsize(file_path) / (1024 * 1024)
if size_mb > max_size_mb:
result['valid'] = False
result['errors'].append(f'File size ({size_mb:.2f}MB) exceeds limit ({max_size_mb}MB)')
return result
# 5. Batch File Operations
def read_multiple_files(file_paths):
"""
Read multiple text files
Args:
file_paths: List of file paths
Returns:
dict: Map of file paths to their contents
"""
results = {}
for file_path in file_paths:
content = read_text_file(file_path)
if content is not None:
results[file_path] = content
return results
def batch_write_files(file_data_map):
"""
Write to multiple files
Args:
file_data_map: Dictionary mapping file paths to content
Returns:
tuple: (successful_files, failed_files)
"""
import os
successful = []
failed = []
for file_path, content in file_data_map.items():
if write_text_file(file_path, content):
successful.append(file_path)
else:
failed.append(file_path)
return successful, failed
def search_in_files(file_paths, search_text, case_sensitive=False):
"""
Search for text in multiple files
Args:
file_paths: List of file paths
search_text: Text to search for
case_sensitive: Whether search is case sensitive
Returns:
dict: Files with their matching lines
"""
results = {}
for file_path in file_paths:
lines = read_text_file_line_by_line(file_path)
if lines:
matching_lines = []
for i, line in enumerate(lines, 1):
search_line = line if case_sensitive else line.lower()
search_term = search_text if case_sensitive else search_text.lower()
if search_term in search_line:
matching_lines.append({'line_number': i, 'content': line})
if matching_lines:
results[file_path] = matching_lines
return results
# 6. File Encoding Utilities
def detect_file_encoding(file_path, encodings=['utf-8', 'latin-1', 'cp1252']):
"""
Detect file encoding by trying different encodings
Args:
file_path: Path to the file
encodings: List of encodings to try
Returns:
str: Detected encoding or None
"""
for encoding in encodings:
try:
with open(file_path, 'r', encoding=encoding) as file:
file.read(1024) # Try reading first 1KB
return encoding
except (UnicodeDecodeError, IOError):
continue
return None
def convert_file_encoding(input_path, output_path, from_encoding, to_encoding='utf-8'):
"""
Convert file from one encoding to another
Args:
input_path: Input file path
output_path: Output file path
from_encoding: Source encoding
to_encoding: Target encoding
Returns:
bool: True if successful
"""
try:
with open(input_path, 'r', encoding=from_encoding) as input_file:
content = input_file.read()
with open(output_path, 'w', encoding=to_encoding) as output_file:
output_file.write(content)
return True
except Exception as e:
print(f"Error converting file: {e}")
return False
# Usage Examples
def demonstrate_text_file_operations():
print("=== Web Python Text File Read/Write Examples ===\n")
# 1. Read text file
print("--- 1. Read Text File ---")
content = read_text_file("example.txt")
if content:
print(f"Content length: {len(content)} characters")
# 2. Read line by line
print("\n--- 2. Read Line by Line ---")
lines = read_text_file_line_by_line("example.txt")
if lines:
print(f"Number of lines: {len(lines)}")
print("First 3 lines:", lines[:3])
# 3. Write text file
print("\n--- 3. Write Text File ---")
if write_text_file("output.txt", "Hello, World!\nThis is a new file."):
print("File written successfully")
# 4. Write lines
print("\n--- 4. Write Lines ---")
lines_to_write = ["Line 1", "Line 2", "Line 3"]
if write_text_lines("lines.txt", lines_to_write):
print("Lines file written successfully")
# 5. Get file details
print("\n--- 5. File Details ---")
details = get_file_details("example.txt")
if details:
print(f"Name: {details['name']}")
print(f"Size: {details['readable_size']}")
print(f"Extension: {details['extension']}")
# 6. File validation
print("\n--- 6. File Validation ---")
validation = validate_file("example.txt", max_size_mb=1)
print(f"Valid: {validation['valid']}")
print("\n=== All Text File Operations Completed ===")
# Export functions
export { read_text_file, read_text_file_with_encoding, read_text_file_line_by_line, read_text_file_in_chunks }
export { write_text_file, write_text_lines, write_text_file_with_encoding, append_to_text_file }
export { get_file_size, get_file_extension, get_file_name_without_extension, format_file_size, get_file_details }
export { file_exists, is_file, is_directory, validate_file }
export { read_multiple_files, batch_write_files, search_in_files }
export { detect_file_encoding, convert_file_encoding }
export { demonstrate_text_file_operations }
💻 Dateien Kopieren/Verschieben python
🟡 intermediate
⭐⭐⭐
Dateien mit Fortschrittsverfolgung und Fehlerbehandlung im Browser kopieren und verschieben
⏱️ 25 min
🏷️ python, web, file operations
Prerequisites:
Intermediate Python, shutil module
# Web Python File Copy/Move Examples
# Browser-based file operations using Python
# 1. File Copy Operations
import shutil
import os
def copy_file(source_path, destination_path):
"""
Copy a file from source to destination
Args:
source_path: Path to source file
destination_path: Path to destination
Returns:
bool: True if successful
"""
try:
shutil.copy2(source_path, destination_path)
return True
except FileNotFoundError:
print(f"Error: Source file '{source_path}' not found")
return False
except PermissionError:
print(f"Error: Permission denied")
return False
except IOError as e:
print(f"Error copying file: {e}")
return False
def copy_file_with_metadata(source_path, destination_path):
"""
Copy file preserving metadata
Args:
source_path: Path to source file
destination_path: Path to destination
Returns:
bool: True if successful
"""
try:
shutil.copy2(source_path, destination_path)
# Preserve timestamps
stat_info = os.stat(source_path)
os.utime(destination_path, (stat_info.st_atime, stat_info.st_mtime))
return True
except Exception as e:
print(f"Error copying file with metadata: {e}")
return False
def copy_directory(source_dir, destination_dir):
"""
Copy entire directory recursively
Args:
source_dir: Source directory path
destination_dir: Destination directory path
Returns:
bool: True if successful
"""
try:
shutil.copytree(source_dir, destination_dir)
return True
except FileNotFoundError:
print(f"Error: Source directory '{source_dir}' not found")
return False
except FileExistsError:
print(f"Error: Destination directory '{destination_dir}' already exists")
return False
except Exception as e:
print(f"Error copying directory: {e}")
return False
def copy_directory_with_progress(source_dir, destination_dir, callback=None):
"""
Copy directory with progress callback
Args:
source_dir: Source directory path
destination_dir: Destination directory path
callback: Progress callback function
Returns:
bool: True if successful
"""
try:
# Count total files first
total_files = sum(len(files) for _, _, files in os.walk(source_dir))
copied_files = 0
for root, dirs, files in os.walk(source_dir):
# Create corresponding directory structure
rel_path = os.path.relpath(root, source_dir)
dest_dir = os.path.join(destination_dir, rel_path)
os.makedirs(dest_dir, exist_ok=True)
# Copy files
for file in files:
source_file = os.path.join(root, file)
dest_file = os.path.join(dest_dir, file)
shutil.copy2(source_file, dest_file)
copied_files += 1
if callback:
callback(copied_files, total_files, file)
return True
except Exception as e:
print(f"Error copying directory with progress: {e}")
return False
# 2. File Move Operations
def move_file(source_path, destination_path):
"""
Move a file from source to destination
Args:
source_path: Path to source file
destination_path: Path to destination
Returns:
bool: True if successful
"""
try:
shutil.move(source_path, destination_path)
return True
except FileNotFoundError:
print(f"Error: Source file '{source_path}' not found")
return False
except PermissionError:
print(f"Error: Permission denied")
return False
except Exception as e:
print(f"Error moving file: {e}")
return False
def move_directory(source_dir, destination_dir):
"""
Move directory from source to destination
Args:
source_dir: Source directory path
destination_dir: Destination directory path
Returns:
bool: True if successful
"""
try:
shutil.move(source_dir, destination_dir)
return True
except Exception as e:
print(f"Error moving directory: {e}")
return False
def rename_file(old_path, new_path):
"""
Rename a file
Args:
old_path: Current file path
new_path: New file path
Returns:
bool: True if successful
"""
return move_file(old_path, new_path)
def rename_directory(old_path, new_path):
"""
Rename a directory
Args:
old_path: Current directory path
new_path: New directory path
Returns:
bool: True if successful
"""
return move_directory(old_path, new_path)
# 3. Batch File Operations
def batch_rename_files(directory, pattern, replacement):
"""
Batch rename files in a directory
Args:
directory: Directory path
pattern: Pattern to replace (supports regex)
replacement: Replacement string
Returns:
tuple: (renamed_files, failed_files)
"""
import re
renamed = []
failed = []
try:
files = os.listdir(directory)
for file in files:
old_path = os.path.join(directory, file)
# Skip directories
if os.path.isdir(old_path):
continue
# Apply regex replacement
new_name = re.sub(pattern, replacement, file)
new_path = os.path.join(directory, new_name)
# Rename file
try:
os.rename(old_path, new_path)
renamed.append((file, new_name))
except Exception as e:
print(f"Error renaming {file}: {e}")
failed.append(file)
return renamed, failed
except Exception as e:
print(f"Error in batch rename: {e}")
return [], failed
def batch_convert_files(source_files, target_directory, conversion_func):
"""
Batch convert files (read, process, write)
Args:
source_files: List of source file paths
target_directory: Target directory path
conversion_func: Function to convert content
Returns:
tuple: (converted_files, failed_files)
"""
import os
os.makedirs(target_directory, exist_ok=True)
converted = []
failed = []
for source_file in source_files:
try:
# Read source file
with open(source_file, 'r', encoding='utf-8') as f:
content = f.read()
# Apply conversion
converted_content = conversion_func(content)
# Write to target
file_name = os.path.basename(source_file)
target_file = os.path.join(target_directory, file_name)
with open(target_file, 'w', encoding='utf-8') as f:
f.write(converted_content)
converted.append((source_file, target_file))
except Exception as e:
print(f"Error converting {source_file}: {e}")
failed.append(source_file)
return converted, failed
# 4. File Synchronization
def sync_directories(source_dir, target_dir):
"""
Synchronize two directories (copy newer files from source to target)
Args:
source_dir: Source directory path
target_dir: Target directory path
Returns:
dict: Sync statistics
"""
stats = {
'copied': 0,
'skipped': 0,
'failed': 0
}
try:
for root, dirs, files in os.walk(source_dir):
rel_path = os.path.relpath(root, source_dir)
dest_dir = os.path.join(target_dir, rel_path)
os.makedirs(dest_dir, exist_ok=True)
for file in files:
source_file = os.path.join(root, file)
dest_file = os.path.join(dest_dir, file)
# Check if file exists in target
if os.path.exists(dest_file):
source_mtime = os.path.getmtime(source_file)
dest_mtime = os.path.getmtime(dest_file)
if source_mtime <= dest_mtime:
stats['skipped'] += 1
continue
# Copy newer file
try:
shutil.copy2(source_file, dest_file)
stats['copied'] += 1
except Exception as e:
print(f"Error syncing {file}: {e}")
stats['failed'] += 1
return stats
except Exception as e:
print(f"Error syncing directories: {e}")
return stats
# 5. Backup and Restore
def create_backup(source_paths, backup_dir, timestamp=True):
"""
Create backup of files/directories
Args:
source_paths: List of source paths to backup
backup_dir: Backup directory path
timestamp: Whether to add timestamp to backup name
Returns:
str: Path to backup directory
"""
from datetime import datetime
try:
# Create backup directory name
if timestamp:
backup_name = datetime.now().strftime('%Y%m%d_%H%M%S')
backup_path = os.path.join(backup_dir, f'backup_{backup_name}')
else:
backup_path = os.path.join(backup_dir, 'backup')
os.makedirs(backup_path, exist_ok=True)
# Copy each source to backup
for source in source_paths:
dest = os.path.join(backup_path, os.path.basename(source))
if os.path.isdir(source):
shutil.copytree(source, dest, dirs_exist_ok=True)
else:
shutil.copy2(source, dest)
return backup_path
except Exception as e:
print(f"Error creating backup: {e}")
return None
def restore_from_backup(backup_dir, target_dir):
"""
Restore files from backup directory
Args:
backup_dir: Backup directory path
target_dir: Target directory to restore to
Returns:
bool: True if successful
"""
try:
if os.path.isdir(backup_dir):
shutil.copytree(backup_dir, target_dir, dirs_exist_ok=True)
else:
shutil.copy2(backup_dir, target_dir)
return True
except Exception as e:
print(f"Error restoring from backup: {e}")
return False
# 6. Safe File Operations
def safe_copy(source_path, destination_path):
"""
Copy file with safety checks
Args:
source_path: Source file path
destination_path: Destination file path
Returns:
bool: True if successful
"""
# Validate source
if not os.path.exists(source_path):
print(f"Error: Source file '{source_path}' does not exist")
return False
if not os.path.isfile(source_path):
print(f"Error: Source path is not a file")
return False
# Check if destination directory exists
dest_dir = os.path.dirname(destination_path)
if dest_dir and not os.path.exists(dest_dir):
try:
os.makedirs(dest_dir, exist_ok=True)
except Exception as e:
print(f"Error creating destination directory: {e}")
return False
# Check if destination already exists
if os.path.exists(destination_path):
print(f"Warning: Destination file '{destination_path}' already exists")
return False
# Perform copy
return copy_file(source_path, destination_path)
def safe_move(source_path, destination_path):
"""
Move file with safety checks
Args:
source_path: Source file path
destination_path: Destination file path
Returns:
bool: True if successful
"""
# Validate source
if not os.path.exists(source_path):
print(f"Error: Source file '{source_path}' does not exist")
return False
if not os.path.isfile(source_path):
print(f"Error: Source path is not a file")
return False
# Check if destination directory exists
dest_dir = os.path.dirname(destination_path)
if dest_dir and not os.path.exists(dest_dir):
try:
os.makedirs(dest_dir, exist_ok=True)
except Exception as e:
print(f"Error creating destination directory: {e}")
return False
# Perform move
return move_file(source_path, destination_path)
# Usage Examples
def demonstrate_file_copy_move():
print("=== Web Python File Copy/Move Examples ===\n")
# 1. Copy file
print("--- 1. Copy File ---")
if safe_copy("source.txt", "copy.txt"):
print("File copied successfully")
# 2. Copy directory
print("\n--- 2. Copy Directory ---")
if copy_directory_with_progress(
"source_dir",
"backup_dir",
lambda current, total, file: print(f"Copying {file}: {current}/{total}")
):
print("Directory copied successfully")
# 3. Move file
print("\n--- 3. Move File ---")
if safe_move("old_name.txt", "new_name.txt"):
print("File moved successfully")
# 4. Batch rename
print("\n--- 4. Batch Rename ---")
renamed, failed = batch_rename_files(".", r"\.(txt)$", ".bak")
print(f"Renamed {len(renamed)} files")
print(f"Failed {len(failed)} files")
# 5. Sync directories
print("\n--- 5. Sync Directories ---")
stats = sync_directories("source_dir", "target_dir")
print(f"Copied: {stats['copied']}, Skipped: {stats['skipped']}, Failed: {stats['failed']}")
# 6. Backup
print("\n--- 6. Backup ---")
backup_path = create_backup(["important_file.txt", "important_dir"], "backups")
if backup_path:
print(f"Backup created at: {backup_path}")
print("\n=== All File Copy/Move Examples Completed ===")
# Export functions
export { copy_file, copy_file_with_metadata, copy_directory, copy_directory_with_progress }
export { move_file, move_directory, rename_file, rename_directory }
export { batch_rename_files, batch_convert_files }
export { sync_directories }
export { create_backup, restore_from_backup }
export { safe_copy, safe_move }
export { demonstrate_file_copy_move }
💻 Verzeichnisdurchlauf python
🔴 complex
⭐⭐⭐⭐
Verzeichnisse mit os.walk, glob patterns und pathlib durchlaufen
⏱️ 30 min
🏷️ python, web, file operations, directory
Prerequisites:
Advanced Python, os module, glob
# Web Python Directory Traversal Examples
# Using os.walk, glob, and pathlib for directory operations
# 1. Directory Listing with os.walk
def list_directory_contents(directory, recursive=False):
"""
List contents of a directory
Args:
directory: Directory path
recursive: Whether to list recursively
Returns:
dict: Directory contents with files and subdirectories
"""
import os
if not os.path.exists(directory):
print(f"Error: Directory '{directory}' does not exist")
return None
result = {
'directory': directory,
'files': [],
'directories': []
}
if recursive:
for root, dirs, files in os.walk(directory):
for dir_name in dirs:
full_path = os.path.join(root, dir_name)
result['directories'].append(full_path)
for file_name in files:
full_path = os.path.join(root, file_name)
result['files'].append(full_path)
else:
try:
with os.scandir(directory) as entries:
for entry in entries:
if entry.is_file():
result['files'].append(os.path.join(directory, entry.name))
elif entry.is_dir():
result['directories'].append(os.path.join(directory, entry.name))
except Exception as e:
print(f"Error listing directory: {e}")
return result
def list_directory_tree(directory, max_depth=None):
"""
List directory as a tree structure
Args:
directory: Root directory path
max_depth: Maximum depth to traverse (None = unlimited)
Returns:
dict: Tree structure
"""
import os
def build_tree(path, depth=0):
if max_depth is not None and depth > max_depth:
return {'name': os.path.basename(path), 'children': []}
node = {
'name': os.path.basename(path),
'path': path,
'is_dir': os.path.isdir(path),
'children': []
}
if os.path.isdir(path):
try:
entries = sorted(os.listdir(path))
for entry in entries:
child_path = os.path.join(path, entry)
child_node = build_tree(child_path, depth + 1)
node['children'].append(child_node)
except PermissionError:
pass # Skip directories we can't access
return node
return build_tree(directory)
def print_directory_tree(tree, indent=0):
"""
Print directory tree structure
Args:
tree: Tree structure from list_directory_tree
indent: Indentation level
"""
prefix = ' ' * indent
icon = '📁' if tree['is_dir'] else '📄'
print(f"{prefix}{icon} {tree['name']}")
for child in tree.get('children', []):
print_directory_tree(child, indent + 1)
# 2. File Pattern Matching with glob
import glob
def find_files_by_pattern(directory, pattern):
"""
Find files matching a pattern
Args:
directory: Directory to search
pattern: Glob pattern (e.g., '*.txt', '**/*.py')
Returns:
list: List of matching file paths
"""
search_pattern = os.path.join(directory, pattern)
return glob.glob(search_pattern, recursive=True)
def find_files_by_extension(directory, extension):
"""
Find files with specific extension
Args:
directory: Directory to search
extension: File extension (e.g., 'txt', 'py')
Returns:
list: List of matching file paths
"""
pattern = f"*.{extension}"
return find_files_by_pattern(directory, pattern)
def find_files_by_name(directory, name_pattern):
"""
Find files by name pattern
Args:
directory: Directory to search
name_pattern: Name pattern (supports wildcards)
Returns:
list: List of matching file paths
"""
import os
matching_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if fnmatch.fnmatch(file, name_pattern):
matching_files.append(os.path.join(root, file))
return matching_files
# 3. Directory Statistics
def get_directory_statistics(directory):
"""
Get statistics about a directory
Args:
directory: Directory path
Returns:
dict: Directory statistics
"""
import os
stats = {
'total_files': 0,
'total_directories': 0,
'total_size': 0,
'extension_counts': {},
'largest_files': []
}
try:
for root, dirs, files in os.walk(directory):
stats['total_directories'] += len(dirs)
for file in files:
file_path = os.path.join(root, file)
stats['total_files'] += 1
try:
file_size = os.path.getsize(file_path)
stats['total_size'] += file_size
# Track extensions
ext = os.path.splitext(file)[1].lower()
stats['extension_counts'][ext] = stats['extension_counts'].get(ext, 0) + 1
# Track largest files
stats['largest_files'].append((file_path, file_size))
except OSError:
pass
# Sort largest files
stats['largest_files'] = sorted(
stats['largest_files'],
key=lambda x: x[1],
reverse=True
)[:10]
# Format size
stats['formatted_size'] = format_file_size(stats['total_size'])
return stats
except Exception as e:
print(f"Error getting directory statistics: {e}")
return None
def format_file_size(bytes_size):
"""Format file size in human-readable format"""
for unit in ['B', 'KB', 'MB', 'GB']:
if bytes_size < 1024.0:
return f"{bytes_size:.2f} {unit}"
bytes_size /= 1024.0
return f"{bytes_size:.2f} TB"
# 4. File Search in Directory
def search_in_directory(directory, search_text, file_pattern='*'):
"""
Search for text in files within a directory
Args:
directory: Directory to search
search_text: Text to search for
file_pattern: Pattern to filter files
Returns:
dict: Files containing the search text
"""
import os
results = {}
for root, dirs, files in os.walk(directory):
for file in files:
if fnmatch.fnmatch(file, file_pattern):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
if search_text in content:
# Find line numbers
lines = content.split('\n')
matches = []
for i, line in enumerate(lines, 1):
if search_text in line:
matches.append(i)
results[file_path] = matches
except (UnicodeDecodeError, IOError):
pass # Skip files that can't be read as text
return results
def search_in_directory_regex(directory, pattern, file_pattern='*'):
"""
Search for regex pattern in files within a directory
Args:
directory: Directory to search
pattern: Regular expression pattern
file_pattern: Pattern to filter files
Returns:
dict: Files containing matches
"""
import os
import re
results = {}
compiled_pattern = re.compile(pattern)
for root, dirs, files in os.walk(directory):
for file in files:
if fnmatch.fnmatch(file, file_pattern):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
matches = []
for match in compiled_pattern.finditer(content):
matches.append({
'start': match.start(),
'end': match.end(),
'text': match.group()
})
if matches:
results[file_path] = matches
except (UnicodeDecodeError, IOError):
pass
return results
# 5. Directory Comparison
def compare_directories(dir1, dir2):
"""
Compare two directories and find differences
Args:
dir1: First directory path
dir2: Second directory path
Returns:
dict: Comparison results
"""
import os
import filecmp
result = {
'only_in_dir1': [],
'only_in_dir2': [],
'different': [],
'same': []
}
# Get all files in both directories
files1 = set()
files2 = set()
for root, dirs, files in os.walk(dir1):
for file in files:
rel_path = os.path.relpath(os.path.join(root, file), dir1)
files1.add(rel_path)
for root, dirs, files in os.walk(dir2):
for file in files:
rel_path = os.path.relpath(os.path.join(root, file), dir2)
files2.add(rel_path)
# Find files only in dir1
result['only_in_dir1'] = list(files1 - files2)
# Find files only in dir2
result['only_in_dir2'] = list(files2 - files1)
# Compare common files
common_files = files1 & files2
for rel_path in common_files:
file1 = os.path.join(dir1, rel_path)
file2 = os.path.join(dir2, rel_path)
if os.path.isfile(file1) and os.path.isfile(file2):
if filecmp.cmp(file1, file2, shallow=False):
result['same'].append(rel_path)
else:
result['different'].append(rel_path)
return result
# 6. Directory Cleanup
def cleanup_directory(directory, dry_run=True, patterns=None):
"""
Clean up directory by removing files matching patterns
Args:
directory: Directory path
dry_run: If True, only print what would be deleted
patterns: List of glob patterns to delete
Returns:
dict: Cleanup results
"""
import os
if patterns is None:
patterns = ['*.tmp', '*.log', '*.bak', '*~']
results = {
'deleted': [],
'failed': []
}
for pattern in patterns:
search_pattern = os.path.join(directory, pattern)
files = glob.glob(search_pattern, recursive=True)
for file in files:
try:
if dry_run:
print(f"Would delete: {file}")
results['deleted'].append(file)
else:
if os.path.isfile(file):
os.remove(file)
elif os.path.isdir(file):
os.rmdir(file)
results['deleted'].append(file)
except Exception as e:
print(f"Error deleting {file}: {e}")
results['failed'].append(file)
return results
def remove_empty_directories(directory, recursive=False):
"""
Remove empty directories
Args:
directory: Directory path
recursive: Whether to recursively remove empty subdirectories
Returns:
int: Number of directories removed
"""
import os
removed_count = 0
if recursive:
# Process subdirectories first (bottom-up)
for root, dirs, files in os.walk(directory, topdown=False):
for dir_name in dirs:
dir_path = os.path.join(root, dir_name)
try:
os.rmdir(dir_path)
removed_count += 1
print(f"Removed empty directory: {dir_path}")
except OSError:
pass # Directory not empty
# Remove the root directory if it's empty
try:
if not os.listdir(directory):
os.rmdir(directory)
removed_count += 1
print(f"Removed empty directory: {directory}")
except OSError:
pass
return removed_count
# Usage Examples
def demonstrate_directory_traversal():
print("=== Web Python Directory Traversal Examples ===\n")
# 1. List directory contents
print("--- 1. List Directory Contents ---")
contents = list_directory_contents('.')
if contents:
print(f"Files: {len(contents['files'])}")
print(f"Directories: {len(contents['directories'])}")
# 2. Directory tree
print("\n--- 2. Directory Tree ---")
tree = list_directory_tree('.')
print_directory_tree(tree)
# 3. Find files by pattern
print("\n--- 3. Find Files by Pattern ---")
txt_files = find_files_by_extension('.', 'txt')
print(f"Text files: {len(txt_files)}")
# 4. Directory statistics
print("\n--- 4. Directory Statistics ---")
stats = get_directory_statistics('.')
if stats:
print(f"Total files: {stats['total_files']}")
print(f"Total size: {stats['formatted_size']}")
print(f"Extensions: {stats['extension_counts']}")
# 5. Search in directory
print("\n--- 5. Search in Directory ---")
search_results = search_in_directory('.', 'TODO', '*.txt')
print(f"Files containing 'TODO': {len(search_results)}")
# 6. Compare directories
print("\n--- 6. Compare Directories ---")
comparison = compare_directories('dir1', 'dir2')
print(f"Only in dir1: {len(comparison['only_in_dir1'])}")
print(f"Only in dir2: {len(comparison['only_in_dir2'])}")
print(f"Different: {len(comparison['different'])}")
# 7. Cleanup
print("\n--- 7. Cleanup ---")
cleanup_results = cleanup_directory('.', dry_run=True)
print(f"Would delete {len(cleanup_results['deleted'])} files")
print("\n=== All Directory Traversal Examples Completed ===")
# Export functions
export { list_directory_contents, list_directory_tree, print_directory_tree }
export { find_files_by_pattern, find_files_by_extension, find_files_by_name }
export { get_directory_statistics, format_file_size }
export { search_in_directory, search_in_directory_regex }
export { compare_directories }
export { cleanup_directory, remove_empty_directories }
export { demonstrate_directory_traversal }