IOError: (13, 'Permission denied') Python Issue

Hey my problem is that I keep getting a permissions denied error when I try to copy a folder directory to a new location. It keeps saying the i dont have permission to the destination directory. And even when I try to remove the original folder I get told my access is denied. Or if I try to move it I get told that “I cant create a file when it already exist”. Even though I’m just trying to move it.

I tried using the os.chmod to give me full permission to both directories but that didnt work. And I tried using the os.stat but I dont fully understand how to use it/ dont think I’m using it the right way.

For an example of what I’m doing:

source= ‘c:\documents and settings\Rock’
dest= ‘c:\documents and settings\Ground’
os.chmod( source, 0777)

When I do.
shutil.move( source, dest)

Error: (183, ‘Cannot create a file when that file already exists’, u’c:\documents and settings\Rock’)

WindowsError: (183, ‘Cannot create a file when that file already exists’, u’c:\documents and settings\Rock’)

And when I do.
shutil.copy( source, dest)
or
shutil.copy2( source, dest)

Error: (13, ‘Permission denied’, u’c:\documents and settings\Rock’)

IOError: (13, ‘Permission denied’, u’c:\documents and settings\Rock’)

I have read up on some forums about people with similar or exact problems and it seems to be a Windows issues because I do not recall running to this problem when I was moving folders around on my mac.

I was just wanted to know if anyone had a work around from this that I’m not seeing or if there is a step that I’m just missing. Any help is well appreciated, thank you.

If Those are actual paths, you need to use double backslashes \, or forward slashes / in them.

If you are doing any file access on files in those directories, you will need to close it before trying to move stuff around.

[QUOTE=btribble;9175]If Those are actual paths, you need to use double backslashes \, or forward slashes / in them.

If you are doing any file access on files in those directories, you will need to close it before trying to move stuff around.[/QUOTE]

ya i was using double \… I’m not to sure what you mean about closing them cuz they are never opened in general

Hello,

couple of things.

If the destination directory already exists, correct, you will get an error on moving the folder. As it states, it already exists. The OS (like windows) will typically throw up that nice do you still want to do this message box and you can answer appropriately.

The shutil.move command has no such functionality, but you could easily roll your own by capturing the exception and prompting the user, OR…

If you don’t care about the collison you can simply copy the folder, but not via shutil.copy* because that is for individual files =)

try shutil.copytree(src, dest)

Something to note: copytree will RECURSIVELY copy the entire folder and sub-folders.

Two ways to prevent that (if so desired), use the shutil.ignore_patterns (IIRC) to block glob patterns of filenames, OR

roll your own copy folder command so you can os.listdir or os.walk the directory and copy discretely.

HTH

[QUOTE=btribble;9175]If Those are actual paths, you need to use double backslashes \, or forward slashes / in them.

If you are doing any file access on files in those directories, you will need to close it before trying to move stuff around.[/QUOTE]

you should “try” to use os.path.sep instead of manually inserting path seperators.

It keeps the code more portable.

also os.path.normpath to normailze the paths for whatever platform you are using.

Just my 0.02 =)

[QUOTE=Amorano;9232]you should “try” to use os.path.sep instead of manually inserting path seperators.

It keeps the code more portable.

also os.path.normpath to normailze the paths for whatever platform you are using.

Just my 0.02 =)[/QUOTE]

thank you for all the help, I will definitely try that solution out I dont know why i wasn’t think of shutil.copytree before. i eventually was able to think of a work around by making a folder in the directory that i want and moving the contents of the old folder into the new folder and then just removing the old folder.