Skip to content

Instantly share code, notes, and snippets.

@enjoythecode
Created December 2, 2020 07:51
Show Gist options
  • Save enjoythecode/339bcf5b641b1c7760f322b64974f6fb to your computer and use it in GitHub Desktop.
Save enjoythecode/339bcf5b641b1c7760f322b64974f6fb to your computer and use it in GitHub Desktop.
Python3 script to delete all .md files in a folder and all of its subdirectories
# Copyright 2020 Sinan Yumurtacı <sinan.yumurtaci@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# Usage: put this file in the root folder of your vault, and run it with
# python3 emptynomore.py
# Use at your own risk. This script has not been tested, and I've coded it for my personal usage, not wide usage.
# Always use backups before modifying important documents such as your Obsidian vault.
import re
import os
links = []
filenames = []
total_link_count = 0
created = 0
for subdir, dirs, files in os.walk("./"):
for file in files:
filenames.append(file)
if ".md" in file:
with open(os.path.join(subdir,file), "r") as content:
length = 0
for line in content.readlines():
length += len(line)
if length == 0 and ".md" in file:
os.remove(os.path.join(subdir, file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment