Re: Concatenating files in order

2017-05-26 Thread Mahmood Naderan via Python-list
Thank you very much. I understand that Regards, Mahmood On Friday, May 26, 2017 5:01 AM, Cameron Simpson wrote: On 25May2017 20:37, Mahmood Naderan wrote: >Cameron, thanks for the points. In fact the file name contains multiple '_' >characters. So, I appreciate what you recommended. > >

Re: Concatenating files in order

2017-05-25 Thread Cameron Simpson
On 25May2017 20:37, Mahmood Naderan 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 ri

Re: Concatenating files in order

2017-05-25 Thread Mahmood Naderan via Python-list
Hi guys, 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(): fi

Re: Concatenating files in order

2017-05-24 Thread Peter Otten
bartc wrote: > On 24/05/2017 16:41, Peter Otten wrote: >> Dennis Lee Bieber wrote: >> >>> On Tue, 23 May 2017 21:42:45 +0100, bartc 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_chu

Re: Concatenating files in order

2017-05-24 Thread bartc
On 24/05/2017 16:41, Peter Otten wrote: Dennis Lee Bieber wrote: On Tue, 23 May 2017 21:42:45 +0100, bartc 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_chu

Re: Concatenating files in order

2017-05-24 Thread Peter Otten
Dennis Lee Bieber wrote: > On Tue, 23 May 2017 21:42:45 +0100, bartc 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 i

Re: Concatenating files in order

2017-05-23 Thread breamoreboy
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 varia

Re: Concatenating files in order

2017-05-23 Thread Tim Chase
On 2017-05-23 13:38, woo...@gmail.com wrote: > It is very straight forward; split on "_", create a new list of > lists that contains a sublist of [file ending as an integer, file > name], and sort > > fnames=["XXX_chunk_0", > "XXX_chunk_10", > "XXX_chunk_1", >

Re: Concatenating files in order

2017-05-23 Thread Cameron Simpson
On 23May2017 21:14, Mahmood Naderan wrote: OK guys thank you very much. It is better to sort them first. Here is what I wrote files = glob.glob('*chunk*') I'd be inclined to go with either '*chunk_*' or just to read the strings from os.listdir, because what you want isn't easily written as

Re: Concatenating files in order

2017-05-23 Thread Erik
On 23/05/17 22:14, Mahmood Naderan via Python-list wrote: sorted=[[int(name.split("_")[-1]), name] for name in files] This isn't sorting anything. At no point do you invoke a sort operation. It's processing the list in the original order and generating a new list with a different format but *

Re: Concatenating files in order

2017-05-23 Thread MRAB
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 y

Re: Concatenating files in order

2017-05-23 Thread Mahmood Naderan via Python-list
OK guys thank you very much. It is better to sort them first. Here is what I wrote files = glob.glob('*chunk*') sorted=[[int(name.split("_")[-1]), name] for name in files] with open('final.txt', 'w') as outf: for fname in sorted: with open(fname[1]) as inf: for line in

Re: Concatenating files in order

2017-05-23 Thread Tim Chase
On 2017-05-23 19:29, Mahmood Naderan via Python-list wrote: > 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

Re: Concatenating files in order

2017-05-23 Thread bartc
On 23/05/2017 20:55, Rob Gaddi wrote: 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. Is it necessary to sort them? If XXX is known, then presumably the first file will be called XXX_chunk_

Re: Concatenating files in order

2017-05-23 Thread woooee
It is very straight forward; split on "_", create a new list of lists that contains a sublist of [file ending as an integer, file name], and sort fnames=["XXX_chunk_0", "XXX_chunk_10", "XXX_chunk_1", "XXX_chunk_20", "XXX_chunk_2"] sorted_lis

Re: Concatenating files in order

2017-05-23 Thread MRAB
On 2017-05-23 21:16, Mahmood Naderan via Python-list wrote: 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 mor

Re: Concatenating files in order

2017-05-23 Thread Grant Edwards
On 2017-05-23, Mahmood Naderan via Python-list wrote: > import glob; > for f in glob.glob('*chunk*'): > print(f) > > it will print in order. Is that really sorted or it is not guaranteed? https://docs.python.org/2/library/glob.html https://docs.python.org/3/library/glob.html It's in the f

Re: Concatenating files in order

2017-05-23 Thread Mahmood Naderan via Python-list
>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_chun

Re: Concatenating files in order

2017-05-23 Thread Rob Gaddi
On 05/23/2017 12:37 PM, breamore...@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

Concatenating files in order

2017-05-23 Thread Mahmood Naderan via Python-list
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