[Tutor] Re: Copy without overwrite?

Kent Johnson kent_johnson at skillsoft.com
Wed Sep 22 12:11:41 CEST 2004


Hi Bernard,

Congratulations on getting this working! Of course I have a couple of 
suggestions :-)

- I notice you call shutil.copytree() if the destination directory is not 
there. This is fine but not necessary - you could just create the directory 
and call softcopy() again. That would make the program self-contained and 
maybe easier to understand - it wouldn't have the two different paths for 
recursion. OTOH you could consider the call to copytree() an optimization, 
since at that point you know you don't have to check for existence of the 
destination files anymore. Either way is fine, really.

- If you do call copytree() you should change your exception handler. Just 
like softcopy(), copytree() catches IOError and wraps them in a 
shutil.Error. The data in the Error is a list of failures. So for the call 
to copytree(), instead of (or in addition to) catching IOError and creating 
a new error list entry, you should catch Error and extend your list with 
the list the Error contains:

                                 try:
                                         s.copytree( srcname, dstname )
                                         print 'DONE'
                                 except os.error, why:
                                         print 'FAILED'
                                         errors.append( ( srcname, dstname, 
why ) )
                                 except (s.Error), why:
                                         print 'FAILED'
                                         errors.extend( why )

Kent

At 11:05 AM 9/22/2004 +0200, Bernard Lebel wrote:
>Thanks to everyone!
>
>Attached is my final script, in txt format.
>
>
>Bernard
>
>
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list