How can I split a file in python?

前端 未结 9 607
失恋的感觉
失恋的感觉 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条回答
  •  Happy的楠姐
    2020-12-03 08:07

    def split_file(file, prefix, max_size, buffer=1024):
        """
        file: the input file
        prefix: prefix of the output files that will be created
        max_size: maximum size of each created file in bytes
        buffer: buffer size in bytes
    
        Returns the number of parts created.
        """
        with open(file, 'r+b') as src:
            suffix = 0
            while True:
                with open(prefix + '.%s' % suffix, 'w+b') as tgt:
                    written = 0
                    while written < max_size:
                        data = src.read(buffer)
                        if data:
                            tgt.write(data)
                            written += buffer
                        else:
                            return suffix
                    suffix += 1
    
    
    def cat_files(infiles, outfile, buffer=1024):
        """
        infiles: a list of files
        outfile: the file that will be created
        buffer: buffer size in bytes
        """
        with open(outfile, 'w+b') as tgt:
            for infile in sorted(infiles):
                with open(infile, 'r+b') as src:
                    while True:
                        data = src.read(buffer)
                        if data:
                            tgt.write(data)
                        else:
                            break
    

提交回复
热议问题