copying files through Python
Lalit Krishna
lalitkrishna at gmail.com
Sat Feb 16 11:02:39 EST 2008
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')
Tim Chase wrote:
>> I am new to python. Infact started yesterday and feeling out of place.
>> I need to write a program which would transfer files under one folder
>> structure (there are sub folders) to single folder. Can anyone give me
>> some idea like which library files or commands would be suitable for
>> this file transfer task. I am sure its matter of few commands. It
>> would be even more great if I could get some sample code with
>
> The shutils.copytree() command[1] will do what you describe
>
> Depending on your source, and if you want to make the dest writeable
> (such as copying off a CD), you may also need to use os.walk() [2]
> combined with os.chmod(filepath, stat.S_IWRITE) [3]
>
> -tkc
>
> [1]
> http://docs.python.org/lib/module-shutil.html#l2h-2297
>
> [2]
> http://docs.python.org/lib/os-file-dir.html#l2h-2707
>
> [3]
> http://docs.python.org/lib/os-file-dir.html#l2h-2677
More information about the Python-list
mailing list