Skip to content

Instantly share code, notes, and snippets.

@koverholt
Created November 24, 2019 04:42
Show Gist options
  • Select an option

  • Save koverholt/73fe6108230b70e355d17512666c6294 to your computer and use it in GitHub Desktop.

Select an option

Save koverholt/73fe6108230b70e355d17512666c6294 to your computer and use it in GitHub Desktop.
Remove H1 titles from beginning of Markdown files
import os
buff = []
for root, dirs, files in os.walk("."):
for file in files:
if file.endswith(".md"):
path = os.path.join(root, file)
with open(path, "r") as f:
print(path)
for line in f:
if line.strip():
buff.append(line)
# Preserve newlines in file
if not line.strip():
buff.append("\n")
# Strip title at beginning of file
if buff[0].startswith("#"):
buff.pop(0)
# Strip newlines at beginning of file
while buff[0] == "\n":
buff.pop(0)
output = open(path, "w")
output.write(''.join(buff))
output.close()
buff = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment