Can b'\n' or b'\n' be part of a bytestring representation of a unicode filename itself?  If so, this will fail.  I don't see the rationale for doing all this splitting on bytestrings instead of converting to unicode, doing the string-based processing, and then converting back to bytes before returning from this function.