Concatenating files in order
bartc
bc at freeuk.com
Wed May 24 13:08:27 EDT 2017
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
--
bartc
More information about the Python-list
mailing list