Break large file down into multiple files
brianrpsgt1
brianlong at cox.net
Fri Feb 13 02:43:02 EST 2009
On Feb 12, 11:02 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Fri, 13 Feb 2009 04:44:54 -0200, brianrpsgt1 <brianl... at cox.net>
> escribió:
>
> > New to python.... I have a large file that I need to break upinto
> > multiple smallerfiles. I need to break the large fileintosections
> > where there are 65535 lines and then write thosesectionsto seperate
> >files. I am familiar with opening and writingfiles, however, I am
> > struggling with creating thesectionsand writing the different
> >sectionsto their ownfiles.
>
> This function copies at most n lines from fin to fout:
>
> def copylines(fin, fout, n):
> for i, line in enumerate(fin):
> fout.write(line)
> if i+1>=n: break
>
> Now you have to open the source file, create newfilesas needed and
> repeatedly call the above function until the end of source file. You'll
> have to enhace it bit, to know whether there are remaining lines or not.
>
> --
> Gabriel Genellina
Gabriel ::
Thanks for the direction. Do I simply define fin, fout and n as
variables above the def copylines(fin, fout, n): line?
Would it look like this?
fin = open('C:\Path\file')
fout = 'C:\newfile.csv')
n = 65535
def copylines(fin, fout, n):
for i, line in enumerate(fin):
fout.write(line)
if i+1>=n: break
Thanks
B
More information about the Python-list
mailing list