Concatenating files in order

Peter Otten __peter__ at web.de
Wed May 24 11:41:54 EDT 2017


Dennis Lee Bieber wrote:

> On Tue, 23 May 2017 21:42:45 +0100, bartc <bc at freeuk.com> declaimed the
> following:
> 
>>Is it necessary to sort them? If XXX is known, then presumably the first
>>file will be called XXX_chunk_0, the next XXX_chunk_1 and so on.
>>
> 
> XXX_chunk_1
> XXX_chunk_10
> XXX_chunk_2

This is a problem you run into if you do sort the filenames (the wrong way, 
alphabetically). If I understand Bart correctly he suggests something like

with open(DESTFILE, "wb") as outstream:
    for filename in map("XXX_chunk_{}".format, itertools.count()):
        try:
            with open(filename, "rb") as instream:
                shutil.copyfileobj(instream, outstream)
        except FileNotFoundError:
            break





More information about the Python-list mailing list