Give a small python script that recursively renames pdf files by adding '_old' before…
- User
Give a small python script that recursively renames pdf files by adding '_old' before the file extension
Assistant
Here's a Python script that recursively renames PDF files by adding '_old' before the extension:
import os def rename_pdfs_recursive(directory): for root, dirs, files in os.walk(directory): for file in files: if file.lower().endswith('.pdf') and not file.endswith('_old.pdf'): old_path = os.path.join(root, file) new_name = file[:-4] + '_old.pdf' # Remove .pdf and add _old.pdf new_path = os.path.join(root, new_name) try: os.rename(old_path, new_path) print(f"Renamed: {old_path} -> {new_path}") except OSError as e: print(f"Error renaming {old_path}: {e}") # Usage directory_path = "." # Current directory, change as needed rename_pdfs_recursive(directory_path)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.