How to split a text file based on the number of characters in Python

Viewed 511

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?

2 Answers
f = open('YourFile.txt', 'r')
c,d = 0,0
s = f.read()
f.close()
w = ""
for ch in s:
    c = c + 1
    w = w + ch
    if c == 100000:
        d = d + 1
        g = open('test'+str(d)+'.txt', 'w')
        g.write(w)
        g.close()
        w = ""
        c = 0

You seem to be missing the creation of the files in the loop as well as updating the variable sentence for the contents of the current file. How about this:

for word in sentence:
    word = word.replace(",", "")
    character_count = character_count + len(word)
    if character_count < 100000:
        output_sentence = output_sentence + word + " "
    else:
        character_count = 0
        outputName = "test" + fileCount + ".txt"

       with open(outputName, "w") as outputFile:
           outputFile.write(output_sentence)

       fileCount = fileCount + 1
       output_sentence = ''
        
Related