-
-
Save vals/f1c915bcbb9a4f9d8f6c 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
class FileSplitter(dict): | |
"""A class for efficientely writing to several file | |
""" | |
def init(self, output_base): | |
self.output_base = output_base | |
def call(self, num, line): | |
self.write_number(num, line) | |
def write_number(self, num, line): | |
fname = self.output_base.replace("--n--", "%04i" % num) | |
self.write_to_handles(num, line, fname) | |
def write_to_handles(self, num, line, fname): | |
try: | |
out_handle = self[fname] | |
except KeyError: | |
opened = False | |
while not opened: | |
try: | |
out_handle = open(fname, "a") | |
self[fname] = out_handle | |
opened = True | |
except IOError as e: | |
if e.errno == 24: | |
files_to_close = 1000 | |
# print("Closing %i of the %i files" % \</span> | |
# (files_to_close, len(self),)) | |
for (num, handle) in self.items()[:files_to_close]: | |
handle.close() | |
del self[num] | |
pass | |
else: | |
raise e | |
out_handle.write("%s\n" % (line,)) | |
def close(self): | |
"""Close all the opened files. | |
""" | |
for handle in self.itervalues(): | |
handle.close() | |
out_format = "numbers_--n--.txt" | |
splitter = FileSplitter(out_format) | |
in_handle = open("numbers.txt") | |
for line in in_handle: | |
num = int(line) % 3000 | |
splitter(num, line) | |
in_handle.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment