Skip to content

Instantly share code, notes, and snippets.

@robertofraile
Created April 14, 2010 12:01
Show Gist options
  • Save robertofraile/365730 to your computer and use it in GitHub Desktop.
Save robertofraile/365730 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Hashplit 2010-04-14: Split a file into separate files using
# directives of the form # file <filename>
import re
import sys
if len(sys.argv) != 2:
exit("""hashplit: Split a file into sub-files
Usage: hashplit <file>
Identify the start of each subfile inside <file> with a line of the form:
# file <subfile>
""")
in_filename = sys.argv[1]
print("hashplit is reading "+ in_filename)
in_file = open(in_filename, "r")
filename = False
files = {}
for line in in_file:
match = re.match("# file (?P<filename>.*)", line)
if match:
filename = match.group("filename")
if filename:
if filename not in files:
files[filename] = ""
files[filename] += line
for filename in files:
print("hashplit is writing "+ filename)
out_file = open(filename, "w")
out_file.write(files[filename])
out_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment