Skip to content

Instantly share code, notes, and snippets.

@koverholt
Created November 20, 2019 18:12
Show Gist options
  • Select an option

  • Save koverholt/36a7a427181b95a2e2ff1191f04d7617 to your computer and use it in GitHub Desktop.

Select an option

Save koverholt/36a7a427181b95a2e2ff1191f04d7617 to your computer and use it in GitHub Desktop.
Split delimited Markdown files into smaller files
with open("export.md", "r") as f:
buff = []
i = 1
for line in f:
if line.strip() and line.strip() != "---":
buff.append(line)
# Preserve newlines in file
if not line.strip():
buff.append("\n")
# Write out smaller files
if line.strip() == "---":
# Strip newlines at beginning of file
while buff[0] == "\n":
buff.pop(0)
# Strip newlines at end of file
while buff[-1] == "\n":
buff.pop(-1)
output = open("import-" + "%d.txt" % i, "w")
output.write(''.join(buff))
output.close()
i += 1
buff = []
# Write out final small file
# Strip newlines at beginning of file
while buff[0] == "\n":
buff.pop(0)
# Strip newlines at end of file
while buff[-1] == "\n":
buff.pop(-1)
output = open("import-" + "%d.txt" % i, "w")
output.write(''.join(buff))
output.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment