mayankjoin3

photos file count csv

Jun 23rd, 2026
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. import os
  2. import csv
  3. import time
  4. from datetime import datetime
  5.  
  6. # Folder to analyze
  7. TARGET_FOLDER = r"D:\Downloads"
  8.  
  9. # CSV output file
  10. CSV_FILE = r"D:\Downloads\folder_metrics.csv"
  11.  
  12. # Start timer
  13. start_time = time.perf_counter()
  14.  
  15. file_count = 0
  16. folder_count = 0
  17. total_size_bytes = 0
  18.  
  19. # Traverse directory
  20. for root, dirs, files in os.walk(TARGET_FOLDER):
  21.     folder_count += len(dirs)
  22.     file_count += len(files)
  23.  
  24.     for file in files:
  25.         file_path = os.path.join(root, file)
  26.         try:
  27.             total_size_bytes += os.path.getsize(file_path)
  28.         except (OSError, FileNotFoundError):
  29.             pass
  30.  
  31. # Convert bytes to MB
  32. total_size_mb = round(total_size_bytes / (1024 * 1024), 2)
  33.  
  34. # End timer
  35. end_time = time.perf_counter()
  36. execution_time_sec = round(end_time - start_time, 3)
  37.  
  38. # Current timestamp
  39. timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  40.  
  41. # Check if CSV exists
  42. file_exists = os.path.isfile(CSV_FILE)
  43.  
  44. # Append data to CSV
  45. with open(CSV_FILE, mode="a", newline="", encoding="utf-8") as csvfile:
  46.     writer = csv.writer(csvfile)
  47.  
  48.     if not file_exists:
  49.         writer.writerow([
  50.             "Timestamp",
  51.             "File_Count",
  52.             "Folder_Count",
  53.             "Total_Size_MB",
  54.             "Execution_Time_Sec"
  55.         ])
  56.  
  57.     writer.writerow([
  58.         timestamp,
  59.         file_count,
  60.         folder_count,
  61.         total_size_mb,
  62.         execution_time_sec
  63.     ])
  64.  
  65. # Print summary
  66. print(f"Timestamp          : {timestamp}")
  67. print(f"Files              : {file_count:,}")
  68. print(f"Folders            : {folder_count:,}")
  69. print(f"Total Size (MB)    : {total_size_mb:,.2f}")
  70. print(f"Execution Time (s) : {execution_time_sec}")
  71. print(f"Metrics appended to: {CSV_FILE}")
Advertisement
Add Comment
Please, Sign In to add comment