Concatenating files in order

Mahmood Naderan nt_mahmood at yahoo.com
Fri May 26 04:28:25 EDT 2017


Thank you very much. I understand that

 
Regards,
Mahmood


On Friday, May 26, 2017 5:01 AM, Cameron Simpson <cs at zip.com.au> wrote:



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