Skip to content

Instantly share code, notes, and snippets.

@pauljacobson
Last active April 8, 2022 15:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pauljacobson/7e70845b8f2a182df2bfc9f49ff6a078 to your computer and use it in GitHub Desktop.
Save pauljacobson/7e70845b8f2a182df2bfc9f49ff6a078 to your computer and use it in GitHub Desktop.
A Python script to generate Obsidian vault stats
# Huge thanks to Rythm for the original code
# that I adapted for this script
# Source: https://j.mp/2UPeDJh
import os.path
import datetime
FILE_PATH = "/path/to/vault/directory/"
TODAY = datetime.datetime.today()
LAST_UPDATE = "{:%A %d %B, %Y at %H:%M}".format(TODAY)
def thousands(big_number):
"""Reformat big numbers with thousands separators"""
rf_number = "{:,}".format(big_number)
return rf_number
markdownFiles = []
all_files = []
words = 0
for dirpath, dirnames, filenames in os.walk(FILE_PATH):
for filename in [f for f in filenames if f.endswith(".md")]:
markdownFiles.append(os.path.join(dirpath, filename))
# This is probably unnecessary duplication
for dirpath, dirnames, filenames in os.walk(FILE_PATH):
for filename in [af for af in filenames]:
all_files.append(os.path.join(dirpath, filename))
for file in markdownFiles:
with open(file, "r") as f:
words += len(f.read().split())
with open(FILE_PATH + "./_vault stats.md", "w") as f:
f.write(
f"""# Vault Statistics
This vault currently contains **{thousands(words)}** words in **{thousands(len(markdownFiles))}** Markdown files.
The vault contains **{thousands(len(all_files))}** files, in total.
_Last updated on **{LAST_UPDATE}**_
"""
)
print(str(f"{words:,}"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment