How to split large txt file into small txt files by line using python
<p>I have a large txt file contains 1 million lines, I want to split them into small txt files each contains 10 lines, how to do it using python?
I found some related questions and have code like this :</p>

<pre><code>def split_file(filepath, lines=30):

    """Split a file based on a number of lines."""

    path, filename = os.path.split(filepath)

    # filename.split('.') would not work for filenames with more than one .

    basename, ext = os.path.splitext(filename)

    # open input file

    with open(filepath, 'r') as f_in:

        try:
            # open the first output file
            f_out = open(os.path.join(path, '{}_{}{}'.format(basename, 0, ext)), 'w')
            # loop over all lines in the input file, and number them
            for i, line in enumerate(f_in):
                # every time the current line number can be divided by the
                # wanted number of lines, close the output file and open a
                # new one
                if i % lines == 0:
                    f_out.close()
                    f_out = open(os.path.join(path, '{}_{}{}'.format(basename, i, ext)), 'w')
                # write the line to the output file
                f_out.write(line)
        finally:
            # close the last output file
            f_out.close()
</code></pre>

<p>However it only functions in small txt file but does not work in my target file, and does not have error information I don't know why.</p>