Concatenating files in order

Peter Otten __peter__ at web.de
Wed May 24 13:42:12 EDT 2017


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 <bc at freeuk.com> 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 is a problem you run into if you do sort the filenames (the wrong
>> way, alphabetically). If I understand Bart correctly he suggests
>> something like
>>
>> with open(DESTFILE, "wb") as outstream:
>>     for filename in map("XXX_chunk_{}".format, itertools.count()):
>>         try:
>>             with open(filename, "rb") as instream:
>>                 shutil.copyfileobj(instream, outstream)
>>         except FileNotFoundError:
>>             break
>>
> 
> Yes, that sort of thing, provided the first file end with _0, XXX is
> known, and the endings are consecutive and well-formed. I was going to
> post some code but wasn't sure how 'with' dealt with file errors:
> 
>   i = 0
> 
>   while 1:
>       file = "XXX_chunk_"+str(i)
>       print (file)
>       with open(file) as inf:
>         ....
>       i += 1
 
Inside the with suite the author of the context manager gets to decide. 
E. g. you may decide to swallow everyhing:

>>> from contextlib import contextmanager
>>> 
>>> @contextmanager
... def catch():
...    try: yield
...    except: pass
... 
>>> with catch(): 1/0
... 
>>>

For whatever open() returns the reaction is to close the file and propagate 
the error:

>>> with open("/dev/null", "rb") as f: 1/0
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> f.closed
True

However, in the case of a failing open() the file's __enter__ method is not 
yet called and so the behaviour is the same as for the conventional

inf = open(file)





More information about the Python-list mailing list