copying files through Python

Diez B. Roggisch deets at nospam.web.de
Sat Feb 16 12:21:11 EST 2008


Lalit Krishna schrieb:
> Hi this is the code which I wrote till now. It is giving permission 
> denied error for sub folders of source directory. Does anyone have any 
> idea what is going wrong
> 
> import os
> import shutil
> def copytreetosinglefolder(src, dst):
> names = os.listdir(src)
> if (os.path.isdir(dst)==False):
> os.mkdir(dst)
> for name in names:
> srcname = os.path.join(src, name)
> try:
> shutil.copy2(srcname, dst)
> except (IOError, os.error), why:
> print "Can't copy %s to %s: %s" % (`srcname`, `dst`, str(why))
> 
> copytreetosinglefolder('c:\\src','d:\\dest')

Please use a mailer/news-agent that preserves whitespace on the 
beginning of the line, and make sure you don't use tabs but spaces to 
indent.

Apart from that - why don't you use shutil.copytree? Regarding the error 
- are you allowed to copy, can you do it using the shell?

Diez



More information about the Python-list mailing list