#!/usr/bin/env python3 """ Remove extra whitespace from a text file. When I copy/paste text from the web into Obsidian, it's often inserted with a lot of additional whitespace, e.g. text like hello\n\nworld becomes hello\n\n \n\nworld This script cleans up some of this extraneous whitespace for me. """ import re import sys if __name__ == '__main__': try: path = sys.argv[1] except IndexError: sys.exit(f"Usage: {__file__} ") with open(path, "r") as infile: text = infile.read() text = re.sub(r"\n\n\s*\n\n", "\n\n", text) with open(path, "w") as outfile: outfile.write(text)