Concatenating files in order

MRAB python at mrabarnett.plus.com
Tue May 23 18:04:33 EDT 2017


On 2017-05-23 22:14, Mahmood Naderan via Python-list wrote:
> OK guys thank you very much. It is better to sort them first.
> 
> 
> Here is what I wrote
> 
> files =  glob.glob('*chunk*')

Here you're making a list of (index, name) pairs:

> sorted=[[int(name.split("_")[-1]), name] for name in files]

but you haven't sorted them. You also need:

sorted.sort()

> with open('final.txt', 'w') as outf:
>        for fname in sorted:
>              with open(fname[1]) as inf:

An alternative to this:

>         for line in inf:
>           outf.write(line)
>   
is:
           outf.writelines(inf)

> 
> and it works
> Regards,
> Mahmood
> 
[snip]



More information about the Python-list mailing list