import codecs import os file_counter = 1 line_counter = 0 fw = codecs.open("zho." + str(file_counter) + ".txt", "w", encoding = "utf-8", errors = "replace") for filename in os.listdir("."): if filename.endswith(".txt"): print("Starting " + filename) with codecs.open(filename, "r", encoding = "utf-8") as fo: for line in fo: line_counter += 1 fw.write(line) if line_counter > 100000: fw.close() file_counter += 1 line_counter = 0 fw = codecs.open("zho." + str(file_counter) + ".txt", "w", encoding = "utf-8", errors = "replace")