Question

I have some code in Python as follows:

folderPath = 'C:\Program Files (x86)\Program\folder/' + folderName
if not os.path.exists(folderPath):
    shutil.copytree('C:\Program Files (x86)\Program\folder\anotherfolder', folderPath)

The variable folderName is from the user input, essentially my program allows the user to create a folder and then some contents from another folder get copied into the new folder. You can think of it as a file backup system of sorts.

The thing that's odd enough is this. It works perfectly, the folder gets created (as outlined in the shutil docs) and the contents of the other folder gets copied, BUT, an error is raised:

[error] script [ myScript ] stopped with error in line 52
[error] shutil.Error ( ['C:\\Program Files (x86)\\Program\\folder\\anotherfolder', 'C:\\Program 
Files (x86)\\Program\\folder\\test', "[Errno 5] Input/output error: 'C:\\\\Program Files 
(x86)\\\\Program\\\\folder\\\\test'"] )

In this case the folderName I input was 'test'. Line 52 is the shutil.copytree() call.

My script then stops running even though the files all got copied and it worked all well and good.

How do I ignore this (if possible) and carry on with the script? Or how do I fix this, if this may indeed be a problem with my code?

All help is appreciated.

Thanks in advance.

Était-ce utile?

La solution 2

There was a problem with shutil in jython see http://bugs.jython.org/issue1872

However this should not be your problem?

You got a forward slash in your path (/) this can't be good. In order to make your program work do the following.

folderPath = 'C:\Program Files (x86)\Program\folder/' + folderName
if not os.path.exists(folderPath):
    try:
        shutil.copytree('C:\Program Files (x86)\Program\folder\anotherfolder', folderPath)
    except Exception, exc:
        print exc

This will catch the error which is raised. If it happens again you should be able to get more detailed information about the error.

You could also use:

folderPath = 'C:\Program Files (x86)\Program\folder/' + folderName
if not os.path.exists(folderPath):
    try:
        shutil.copytree('C:\Program Files (x86)\Program\folder\anotherfolder', folderPath)
    except Exception:
        import traceback
        traceback.print_exc()

Edit: Be aware of the fact that your problem still exists! The Exception/Error is caught within the except clause, so the program does not crash. See comments and Laur Ivan's answer. This error happens because you don't have the access rights to do something in the Program Files directory.

I hope this helps. Greetings Xeun

Autres conseils

Digging a bit more:

According to microsoft, errno 5 corresponds to access denied.

copytree uses copy2() which copies the file and then changes its permissions, times.

IMHO you don't have the rights to change (or retrieve) file attributes, so you get the file with whatever attributes you have yourself (your user, group...).

HTH

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top