I cannot get shutil to copy one folder to another.
This code results in the error below. What am I doing wrong?

if os.path.exists(backup + "\\Safety Backup"):
    pass
else:
    print "Backing up original music. . ."
    os.mkdir(backup + "\\Safety Backup")
    for folder in musicFolders:
        shutil.copy2("C:\ABCD123", backup + "\\Safety Backup") 
    print "Backup complete."
    os.system("cls")

IOError: [Errno 13] Permission denied: 'C:\\ABCD123'

No...It does this with every folder I try.

I'm on Windows XP by the way.

print the exception, replace shutil.copy2.... with:

try:
  shutil.copy2("C:\ABCD123", backup + "\\Safety Backup") 
except WindowsError as e:
  print ("ERROR: WINDOWSERROR = ",e)
except :
  print ("ERROR: Some other error happened")

This will atleast start the debugging process.

Let us know what it says

I had the error in my first post.
IOError: [Errno 13] Permission denied: 'C:\\ABCD123'

Anyways, changing WindowsError to IOError gives me that same result.

Try this and it should work.

import shutil

shutil.copy('c:\\test\my_file.txt', 'c:\\temp')

You most copy files with shutil.copy.

This will give you and Permission denied.
You can not copy folder to folder with shutil.copy.

import shutil

shutil.copy('c:\\test', 'c:\\temp')

Use copytree.

import shutil

shutil.copytree('c:\\test', 'c:\\new')

This will copy from c:\test and make a new folder c:\new.
It will give and error if c:\new excited

Look also at this for coping folders.
http://docs.python.org/distutils/apiref.html#distutils.dir_util.copy_tree

import os
import distutils.dir_util

distutils.dir_util.copy_tree('c:/test', 'c:/testbak')

Alwas use \\ or /

c:\test  #No \ can be looked at as an escape character
c:\\test #Ok
c:/test  #Ok

There we go! All folders and files copied sucessfully. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.