I realize that you didn't introduce this here, but I think this is going to be really inefficient to open/close a file this many times. What if instead you built a buffer system, where you have a list of lines for each file, and after you've processed e.g. 100k sequences (a ``buffer_size`` parameter, to facilitate unit testing), you open each output file and write the lines that have been generated for that file. If you do that, make sure that you have a clean-up that writes all remaining lines at the end, and have a unit test that include an input number of sequences which is not a multiple of your ``buffer_size``.