How can I split a file in python?

前端 未结 9 595
失恋的感觉
失恋的感觉 2020-12-03 07:29

Is it possible to split a file? For example you have huge wordlist, I want to split it so that it becomes more than one file. How is this possible?

9条回答
  •  离开以前
    2020-12-03 08:14

    This one splits a file up by newlines and writes it back out. You can change the delimiter easily. This can also handle uneven amounts as well, if you don't have a multiple of splitLen lines (20 in this example) in your input file.

    splitLen = 20         # 20 lines per file
    outputBase = 'output' # output.1.txt, output.2.txt, etc.
    
    # This is shorthand and not friendly with memory
    # on very large files (Sean Cavanagh), but it works.
    input = open('input.txt', 'r').read().split('\n')
    
    at = 1
    for lines in range(0, len(input), splitLen):
        # First, get the list slice
        outputData = input[lines:lines+splitLen]
    
        # Now open the output file, join the new slice with newlines
        # and write it out. Then close the file.
        output = open(outputBase + str(at) + '.txt', 'w')
        output.write('\n'.join(outputData))
        output.close()
    
        # Increment the counter
        at += 1
    

提交回复
热议问题