Advertisement
Guest User

Untitled

a guest
Aug 19th, 2024
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import os
  2. import datetime
  3. import pyperclip
  4.  
  5. def get_file_info(file_path):
  6. """Get file information including size and last modified time."""
  7. stats = os.stat(file_path)
  8. return {
  9. 'size': stats.st_size,
  10. 'last_modified': datetime.datetime.fromtimestamp(stats.st_mtime).strftime('%Y-%m-%d %H:%M:%S')
  11. }
  12.  
  13. def should_skip(path):
  14. """Check if the file or directory should be skipped."""
  15. name = os.path.basename(path)
  16. return (
  17. name.startswith('.') or # Hidden files/directories
  18. name == '__pycache__' or # Python cache
  19. name == 'show_codebase.py' # This script itself
  20. )
  21.  
  22. def concatenate_files(directory='.'):
  23. """Recursively concatenate all files in the directory and subdirectories with annotations and return as a string."""
  24. output = []
  25. for root, dirs, files in os.walk(directory):
  26. # Remove directories that should be skipped
  27. dirs[:] = [d for d in dirs if not should_skip(os.path.join(root, d))]
  28.  
  29. for file in files:
  30. if should_skip(file):
  31. continue
  32.  
  33. file_path = os.path.join(root, file)
  34. file_info = get_file_info(file_path)
  35.  
  36. output.append(f"\n\n{'=' * 80}")
  37. output.append(f"File: {file_path}")
  38. output.append(f"Size: {file_info['size']} bytes")
  39. output.append(f"Last Modified: {file_info['last_modified']}")
  40. output.append('=' * 80 + '\n')
  41.  
  42. try:
  43. with open(file_path, 'r', encoding='utf-8') as f:
  44. content = f.read()
  45. output.append(content)
  46. except Exception as e:
  47. output.append(f"Error reading file: {str(e)}")
  48.  
  49. return '\n'.join(output)
  50.  
  51. if __name__ == '__main__':
  52. result = concatenate_files()
  53. print(result)
  54. pyperclip.copy(result)
  55. print("\nOutput has been copied to clipboard.")
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement