Unable to Concatenate a file - destination kept getting overwritten

MRAB python at mrabarnett.plus.com
Fri Apr 20 22:38:55 EDT 2012


On 21/04/2012 02:03, Foster Rilindo wrote:
> I can't seem to concatenate.
>
> I got binary files here:
>
> yvaine:disk rilindo$ ls -lah
> total 61440
> drwxr-xr-x   4 rilindo  staff   136B Apr 20 19:47 .
> drwxr-xr-x  10 rilindo  staff   340B Apr 20 19:45 ..
> -rw-r--r--   1 rilindo  staff    20M Apr 20 20:00 disk1
> -rw-r--r--   1 rilindo  staff    10M Apr 20 19:47 disk2
>
> Based on the following code, I should be able to cat disk2 over to disk 1, resulting in a 30 Meg file:
>
>>>>  import shutil
>>>>  import os
>>>>  disk1="/Users/rilindo/tmp/disk/disk1"
>>>>  disk2="/Users/rilindo/tmp/disk/disk2"
>>>>  destination = open(disk1,'wb')
>>>>  shutil.copyfileobj(open(disk2,'rb'),destination)
>>>>  destination.close()
>
> However, the result is that the destination gets overwritten:
>
> yvaine:disk rilindo$ ls -lah
> total 40960
> drwxr-xr-x   4 rilindo  staff   136B Apr 20 19:47 .
> drwxr-xr-x  10 rilindo  staff   340B Apr 20 19:45 ..
> -rw-r--r--   1 rilindo  staff    10M Apr 20 20:02 disk1
> -rw-r--r--   1 rilindo  staff    10M Apr 20 19:47 disk2
>
> Is this right way to concatenate a file or is there a better way?
>
It's "rb" to read binary, "wb" to write binary, truncating the existing
file, and "ab" to append binary.

You want to append binary.



More information about the Python-list mailing list