Python Split files into multiple smaller files

Reks bot

Write a function named file_split(filename, number_of_files) that will split an input file into a number of output files. The files should be split as evenly as possible. When the file length is evenly divisible by the number of files to create (a 10-line file, split into 2 files, each output file should have 5 lines. When the length is not evenly divisible all output files’ length must not have a difference greater than 1. For example, a file of 10 lines, split in 3, would have output files of length 3, 3 and 4.

I have written my code but I can not figure out how to do the difference greater than 1 part, I need help modifying my code to include that part. (The code I have creates a new file for the last line if it is not an even multiple)

def get_line_counts(filename, number_of_files):
    try:
        my_file = open(filename, 'r')
    except IOError:
        print("File does not exist")
        return    
    input = my_file.read().split('\n')
    outputBase = 'lel'    
    total_lines = 0
    with open('myfile.txt') as infp:
        for line in infp:
            if line.strip():  
                total_lines +=1    
    base_size = total_lines // number_of_files    
    at = 1
    for lines in range(0, len(input), base_size):
        outputData = input[lines:lines+base_size]
        output = open(outputBase + str(at) + '.txt', 'w')
        output.write('\n'.join(outputData))
        output.close()
        at += 1
Stefan Pochmann

Round-robin works and is easy:

with open('myfile.txt') as infp:
    files = [open('%d.txt' % i, 'w') for i in range(number_of_files)]
    for i, line in enumerate(infp):
        files[i % number_of_files].write(line)
    for f in files:
        f.close()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I split a large HAProxy config file into multiple smaller files?

From Dev

rails 4: split routes.rb into multiple smaller files

From Dev

busybox tar or split large file to multiple smaller files

From Dev

Split zip into smaller zip files

From Dev

Split files into even smaller files from a previous split in a directory

From Dev

Split diary file into multiple files using Python

From Dev

Split Large XML File Into Smaller Files

From Dev

How to split larger files into smaller parts?

From Dev

How to split csv files into multiple files using the delimiter? python

From Dev

Split a list of files into multiple smaller lists based on file size in c#

From Dev

Split Flat Files into smaller files (on row count) using Custom Pipeline

From Dev

copy (multiple) files only if filesize is smaller

From Dev

How to split a module into multiple files

From Dev

Split file into multiple files in Excel

From Dev

split file into multiple files (by columns)

From Dev

Split a json array to multiple files

From Dev

Split multiple CSV files in batch

From Dev

Julia: Split module into multiple files

From Dev

How to split build.gradle file into files with smaller tasks

From Java

How to split a large text file into smaller files with equal number of lines?

From Dev

Sphinx: split output html into smaller pages than input files

From Dev

Split text file into smaller files based on size (Windows)

From Dev

XQuery: How to split large xml files into smaller ones

From Dev

How to split XML file into smaller files using Powershell

From Dev

How to split TrueCrypt file into smaller files for cloud backup to OneDrive?

From Dev

Split a CSV file into smaller files based on some condition

From Dev

How to split a file up into many smaller files and then reconstruct them?

From Dev

How to split XML file into smaller files using Powershell

From Dev

How to split build.gradle file into files with smaller tasks

Related Related

  1. 1

    Can I split a large HAProxy config file into multiple smaller files?

  2. 2

    rails 4: split routes.rb into multiple smaller files

  3. 3

    busybox tar or split large file to multiple smaller files

  4. 4

    Split zip into smaller zip files

  5. 5

    Split files into even smaller files from a previous split in a directory

  6. 6

    Split diary file into multiple files using Python

  7. 7

    Split Large XML File Into Smaller Files

  8. 8

    How to split larger files into smaller parts?

  9. 9

    How to split csv files into multiple files using the delimiter? python

  10. 10

    Split a list of files into multiple smaller lists based on file size in c#

  11. 11

    Split Flat Files into smaller files (on row count) using Custom Pipeline

  12. 12

    copy (multiple) files only if filesize is smaller

  13. 13

    How to split a module into multiple files

  14. 14

    Split file into multiple files in Excel

  15. 15

    split file into multiple files (by columns)

  16. 16

    Split a json array to multiple files

  17. 17

    Split multiple CSV files in batch

  18. 18

    Julia: Split module into multiple files

  19. 19

    How to split build.gradle file into files with smaller tasks

  20. 20

    How to split a large text file into smaller files with equal number of lines?

  21. 21

    Sphinx: split output html into smaller pages than input files

  22. 22

    Split text file into smaller files based on size (Windows)

  23. 23

    XQuery: How to split large xml files into smaller ones

  24. 24

    How to split XML file into smaller files using Powershell

  25. 25

    How to split TrueCrypt file into smaller files for cloud backup to OneDrive?

  26. 26

    Split a CSV file into smaller files based on some condition

  27. 27

    How to split a file up into many smaller files and then reconstruct them?

  28. 28

    How to split XML file into smaller files using Powershell

  29. 29

    How to split build.gradle file into files with smaller tasks

HotTag

Archive