-1

I have a problem statement:

splitfile(filename,numberoffiles)

A file of 13 lines, split in 3, would have output files of length 4, 4 and 5 if they cannot be evenly distributed. ( Can not have a difference greater than 1 line if they cant be evenly distributed)

I started learning python and I have to create a function that will split a file into smaller ones as specified in the parameters.

The thing I am having trouble with is I do not know how to approach this scenario due to it being based off of number of files and the concept of having a difference greater than 1 being non permissible.

3
  • 5
    Hello Reks, welcome to Stack Overflow. Please consider taking the time to read the tour page and How to Ask -- as it stands your question is simply a problem statement and doesn't show what specific problem you are facing. Asking SO to write all your code is not appropriate as a question. Show what you've tried to address your problem, and describe clearly the specific issue you face. Commented Jun 1, 2015 at 11:32
  • Check out os.stat() for file size and file.readlines([sizehint]). These are for reading various info meant for the program. I think you know how to do the writing part Commented Jun 1, 2015 at 11:37
  • The problem statement has some clumsy wording. The last part in brackets is only saying that if you split your total number of lines (from the file) into the x number of groups. If the difference in size between any of the groups is more than 1, then your lines have not been equally divided. Commented Jun 1, 2015 at 11:57

1 Answer 1

0

The essence of the question (as I understand it) is how to determine the number of lines that each output file will contain. This is what I came up with for Python 3.4.3:

def get_line_counts(total_lines, number_of_files):
    base_size = total_lines // number_of_files
    line_count_list = [base_size for i in range(number_of_files)]
    files_with_an_extra_line = total_lines % number_of_files
    for i in range(files_with_an_extra_line):
        line_count_list[len(line_count_list) - (i + 1)] += 1
    return line_count_list


for i, n in enumerate(get_line_counts(13, 3)):
    print("file {0} will contain {1} line(s)".format(i, n))

resulting in

file 0 will contain 4 line(s)
file 1 will contain 4 line(s)
file 2 will contain 5 line(s)

The rest of the code would just be basic file I/O: read n lines from an input text file and write them to an output text file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.