Created
August 4, 2025 21:06
-
-
Save kylemcdonald/2888284c75713ed9b3d77cbdeb822492 to your computer and use it in GitHub Desktop.
Python script for showing a histogram of the file creation times of all files in a directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import time | |
| import matplotlib.pyplot as plt | |
| from datetime import datetime | |
| def format_time(epoch_time): | |
| return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(epoch_time)) | |
| # Global figure and axis for single window | |
| fig, ax = None, None | |
| def init_plot(): | |
| global fig, ax | |
| if fig is None: | |
| fig, ax = plt.subplots(figsize=(12, 6)) | |
| plt.ion() # Turn on interactive mode | |
| plt.show(block=False) | |
| def update_histogram(creation_times): | |
| global fig, ax | |
| if not creation_times: | |
| return | |
| init_plot() | |
| # Clear previous plot | |
| ax.clear() | |
| # Convert timestamps to datetime objects for better binning | |
| dates = [datetime.fromtimestamp(ts) for ts in creation_times] | |
| # Create histogram | |
| ax.hist(dates, bins=50, alpha=0.7, color='skyblue', edgecolor='black') | |
| ax.set_title(f'File Creation Time Distribution ({len(creation_times)} files)') | |
| ax.set_xlabel('Creation Date') | |
| ax.set_ylabel('Number of Files') | |
| ax.tick_params(axis='x', rotation=45) | |
| ax.grid(True, alpha=0.3) | |
| # Update the display | |
| plt.tight_layout() | |
| plt.draw() | |
| plt.pause(0.01) | |
| def find_file_times(base_path): | |
| creation_times = [] | |
| last_update_time = 0 | |
| files_processed = 0 | |
| for root, dirs, files in os.walk(base_path): | |
| for file in files: | |
| filepath = os.path.join(root, file) | |
| try: | |
| ctime = os.path.getctime(filepath) | |
| creation_times.append(ctime) | |
| files_processed += 1 | |
| except OSError: | |
| continue | |
| current_time = time.time() | |
| # Update histogram no more than once per second | |
| if current_time - last_update_time >= 1.0: | |
| print(f"Processed {files_processed} files. Updating histogram...") | |
| update_histogram(creation_times) | |
| last_update_time = current_time | |
| # Final update | |
| print(f"Scan complete. Total files processed: {files_processed}") | |
| update_histogram(creation_times) | |
| if creation_times: | |
| oldest = min(creation_times) | |
| newest = max(creation_times) | |
| print(f"\nTime range:") | |
| print(f" Oldest: {format_time(oldest)}") | |
| print(f" Newest: {format_time(newest)}") | |
| if __name__ == "__main__": | |
| base_directory = input("Enter the directory to scan: ").strip() | |
| if os.path.isdir(base_directory): | |
| find_file_times(base_directory) | |
| # Keep the plot window open | |
| input("Press Enter to close...") | |
| else: | |
| print("Invalid directory.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment