Created
May 16, 2022 18:33
-
-
Save stefnestor/001ec0680c8e70c750d79d0b06c6b904 to your computer and use it in GitHub Desktop.
Graphing a Directory's File Statistics
This file contains 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 datetime | |
import math | |
import matplotlib.pyplot as plt # https://matplotlib.org | |
import mplcursors # https://mplcursors.readthedocs.io | |
import numpy as np # https://numpy.org/ | |
import os | |
import sys | |
### usage notes | |
# Method to pull file statitics may depend on OS. | |
# Tested on Mac OS. More info below. | |
### user input | |
CUSTOM_DIRECTORY = "Documents/code/notes" | |
### program | |
FILE_DIRECTORY = os.path.join(os.path.expanduser('~'),CUSTOM_DIRECTORY) | |
FILES = [x for x in os.listdir(FILE_DIRECTORY) if x.endswith(".md")] | |
EPOCH = datetime.datetime(1970,1,1) | |
NOW = int((datetime.datetime.utcnow()-EPOCH).total_seconds()) | |
def days_between(first,second): | |
return math.ceil((first-second)/86400) | |
files_stats = [] | |
for file in FILES: | |
file_path = os.path.join(FILE_DIRECTORY,file) | |
# method to pull stats depends on computer OS, see | |
# https://stackoverflow.com/a/39501288/15146924 | |
file_stats = os.stat(file_path) | |
last_modified = int(file_stats.st_mtime) | |
last_accessed = int(file_stats.st_atime) | |
last_created = int(file_stats.st_birthtime) | |
days_since_modified = days_between(NOW,last_modified) | |
days_since_accessed = days_between(NOW,last_accessed) | |
days_since_created = days_between(NOW,last_created) | |
days_alive = days_between(last_accessed,last_created) | |
days_worked = days_between(last_modified,last_created) | |
files_stats.append([file, | |
last_modified, last_accessed, last_created, | |
days_since_modified, days_since_accessed, days_since_created, | |
days_alive, days_worked]) | |
y = [i[2] for i in files_stats] | |
x = [i[3] for i in files_stats] | |
sizes = [i[1] for i in files_stats] | |
colors = [i[2] for i in files_stats] | |
fig, ax = plt.subplots() | |
plt.ylabel("last_accessed") | |
plt.xlabel("created") | |
ax.scatter(x, y, c=colors) | |
ax.set(xlim=(min(x), max(x)), ylim=(min(y), max(y))) | |
cursor = mplcursors.cursor(ax, hover=True) | |
@cursor.connect("add") | |
def on_add(sel): | |
sel.annotation.set(text=files_stats[sel.target.index][0][:-3]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example described on Medium.