Concatenating files in order

Rob Gaddi rgaddi at highlandtechnology.invalid
Tue May 23 15:55:29 EDT 2017


On 05/23/2017 12:37 PM, breamoreboy at gmail.com wrote:
> On Tuesday, May 23, 2017 at 8:29:57 PM UTC+1, Mahmood Naderan wrote:
>> Hi,
>> There are some text files ending with _chunk_i where 'i' is an integer. For example,
>>
>> XXX_chunk_0
>> XXX_chunk_1
>> ...
>>
>> I want to concatenate them in order. Thing is that the total number of files may be variable. Therefore, I can not specify the number in my python script. It has to be "for all files ending with _chunk_i".
>>
>> Next, I can write
>>
>> with open('final.txt', 'w') as outf:
>> for fname in filenames:
>>              with open(fname) as inf:
>>                  for line in inf:
>>          outf.write(line)
>>   
>>
>> How can I specify the "filenames"?
>> Regards,
>> Mahmood
> 
> https://docs.python.org/3/library/glob.html seems a good place to start.
> 
> Kindest regards.
> 
> Mark Lawrence.
> 

Yup.  Make a list of all the file names, write a key function that 
extracts the numbery bits, sort the list based on that key function, and 
go to town.

Alternatively, when you create the files in the first place, make sure 
to use more leading zeros than you could possibly need. 
xxx_chunk_000001 sorts less than xxx_chunk_000010.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list