Search code examples
pythonfilefile-writing

Function to divide a text file into two files


I wrote a function to input a text file and a ratio (eg. 80%) to divide the first 80% of the file into a file and the other 20% to another file. The first part is correct but the second part is empty. can someone take a look and let me know my mistake?

def splitFile(inputFilePatheName, outputFilePathNameFirst, outputFilePathNameRest, splitRatio):
    lines = 0
    buffer = bytearray(2048)
    with open(inputFilePatheName) as f:
        while f.readinto(buffer) > 0:
            lines += buffer.count('\n')

    print lines
    line80 = int(splitRatio * lines)
    print line80
    with open(inputFilePatheName) as originalFile:
        firstNlines = originalFile.readlines()[0:line80]
        restOfTheLines=originalFile.readlines()[(line80+1):lines]

    print len(firstNlines)
    print len(restOfTheLines)
    with open(outputFilePathNameFirst, 'w') as outputFileNLines:
         for item in firstNlines:
             outputFileNLines.write("{}".format(item))

    with open(outputFilePathNameRest,'w') as outputFileRest:
         for word in restOfTheLines:
             outputFileRest.write("{}".format(word))

Solution

  • I believe this is your problem:

    firstNlines = originalFile.readlines()[0:line80]
    restOfTheLines=originalFile.readlines()[(line80+1):lines]
    

    When you call readlines() the second time, you don't get anything, because you've already read all the lines from the file. Try:

    allLines = originalFile.readlines()
    firstNLines, restOfTheLines = allLines[:line80], allLines[(line80+1):]
    

    Of course, for very large files there is a problem that you are reading the entire file into memory.