Created
April 14, 2010 12:01
-
-
Save robertofraile/365730 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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