Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import datetime
- import pyperclip
- def get_file_info(file_path):
- """Get file information including size and last modified time."""
- stats = os.stat(file_path)
- return {
- 'size': stats.st_size,
- 'last_modified': datetime.datetime.fromtimestamp(stats.st_mtime).strftime('%Y-%m-%d %H:%M:%S')
- }
- def should_skip(path):
- """Check if the file or directory should be skipped."""
- name = os.path.basename(path)
- return (
- name.startswith('.') or # Hidden files/directories
- name == '__pycache__' or # Python cache
- name == 'show_codebase.py' # This script itself
- )
- def concatenate_files(directory='.'):
- """Recursively concatenate all files in the directory and subdirectories with annotations and return as a string."""
- output = []
- for root, dirs, files in os.walk(directory):
- # Remove directories that should be skipped
- dirs[:] = [d for d in dirs if not should_skip(os.path.join(root, d))]
- for file in files:
- if should_skip(file):
- continue
- file_path = os.path.join(root, file)
- file_info = get_file_info(file_path)
- output.append(f"\n\n{'=' * 80}")
- output.append(f"File: {file_path}")
- output.append(f"Size: {file_info['size']} bytes")
- output.append(f"Last Modified: {file_info['last_modified']}")
- output.append('=' * 80 + '\n')
- try:
- with open(file_path, 'r', encoding='utf-8') as f:
- content = f.read()
- output.append(content)
- except Exception as e:
- output.append(f"Error reading file: {str(e)}")
- return '\n'.join(output)
- if __name__ == '__main__':
- result = concatenate_files()
- print(result)
- pyperclip.copy(result)
- print("\nOutput has been copied to clipboard.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement