We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

permission denied when using rmtree

LuckyLucks
LuckyLucks asked
on
Medium Priority
1,902 Views
Last Modified: 2012-05-06
HI

I am using python 2.3 on windows.

Whenever I run shutil.rmtree(mypath), I get the following error
shutil.rmtree(mypath)

File "c:\Python23\lib\shutil.py", line 142, in rmtree^M
826     raise exc[0], (exc[1][0], exc[1][1] + ' removing '+arg)^M
827 OSError: [Errno 13] Permission denied removing c:/temp/mydir

I am not sure why it is doing this. I have all permissions open on the temp drive.
No other program (to my knowledge) is using the same path that I am trying to delete.


"c:\Python23\lib\shutil.py" is pasted below:

def rmtree(path, ignore_errors=False, onerror=None):
    """Recursively delete a directory tree.

    If ignore_errors is set, errors are ignored; otherwise, if
    onerror is set, it is called to handle the error; otherwise, an
    exception is raised.
    """
    cmdtuples = []
    arg = path
    try:
        func = os.listdir # Make sure it isn't unset
        _build_cmdtuple(path, cmdtuples)
        for func, arg in cmdtuples:
            func(arg)
    except OSError:
        exc = sys.exc_info()
        if ignore_errors:
            pass
        elif onerror is not None:
            onerror(func, arg, exc)
        else:
            raise exc[0], (exc[1][0], exc[1][1] + ' removing '+arg)

Comment
Watch Question

CERTIFIED EXPERT
Commented:
The Python 2.5 implementation is rather different (see the snippet). In your case, try to call

shutil.rmtree(mypath, ignore_errors=True)

and have a look at what was not deleted. The reason may be more apparent in the case.

Petr
def rmtree(path, ignore_errors=False, onerror=None):
    """Recursively delete a directory tree.
 
    If ignore_errors is set, errors are ignored; otherwise, if onerror
    is set, it is called to handle the error with arguments (func,
    path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
    path is the argument to that function that caused it to fail; and
    exc_info is a tuple returned by sys.exc_info().  If ignore_errors
    is false and onerror is None, an exception is raised.
 
    """
    if ignore_errors:
        def onerror(*args):
            pass
    elif onerror is None:
        def onerror(*args):
            raise
    names = []
    try:
        names = os.listdir(path)
    except os.error, err:
        onerror(os.listdir, path, sys.exc_info())
    for name in names:
        fullname = os.path.join(path, name)
        try:
            mode = os.lstat(fullname).st_mode
        except os.error:
            mode = 0
        if stat.S_ISDIR(mode):
            rmtree(fullname, ignore_errors, onerror)
        else:
            try:
                os.remove(fullname)
            except os.error, err:
                onerror(os.remove, fullname, sys.exc_info())
    try:
        os.rmdir(path)
    except os.error:
        onerror(os.rmdir, path, sys.exc_info())

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT

Commented:
Possibly, you should think about Python 2.6.1 or at least Python 2.5.4 (i.e. the latest versions).
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.