I have a big file and want to split the text file into multiple files based on their number of characters. Each file has to have characters lower than 100000.
So for example, input file test.txt would turn into ```test1.txt, test2.txt, test3.txt... test1932.txt''' etc.
I have the below logic.
with open("test.txt") as inFile:
sentence = inFile.read().split()
character_count = 0
output_sentence = ""
fileCount = 0
outputName = "test" + fileCount + ".txt"
for word in sentence:
word = word.replace(",", "")
character_count = character_count + len(word)
if character_count < 100000:
output_sentence = output_sentence + word + " "
else:
fileCount = fileCount + 1
break
with open(outputName, "w") as outputFile:
outputFile.write(output_sentence)
However, I am kind of stuck on how to loop this so that it keep generating new files. How do I accomplish this?