Concatenating files in order

Cameron Simpson cs at zip.com.au
Thu May 25 20:25:26 EDT 2017


On 25May2017 20:37, Mahmood Naderan <nt_mahmood at yahoo.com> wrote:
>Cameron, thanks for the points. In fact the file name contains multiple '_' 
>characters. So, I appreciate what you recommended.
>
>  filenames = {}
>  for name in glob.glob('*chunk_*'):
>    left, right = name.rsplit('_', 1)
>    if left.endswith('chunk') and right.isdigit():
>      filenames[int(right)] = filename
>  sorted_filenames = [ filenames[k] for k in sorted(filenames.keys()) ]
>
>It seems that 'filename' should be 'right'.

No, 'filename' should be 'name': the original filename. Thanks for the catch.

The idea is to have a map of int->filename so that you can open the files in 
numeric order.  So 'right' is just the numeric suffix - you need 'name' for the 
open() call.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list