permission denied when using rmtree

Posted on 2009-02-10
Last Modified: 2012-05-06

I am using python 2.3 on windows.

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

File "c:\Python23\lib\", 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\" 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
        func = os.listdir # Make sure it isn't unset
        _build_cmdtuple(path, cmdtuples)
        for func, arg in cmdtuples:
    except OSError:
        exc = sys.exc_info()
        if ignore_errors:
        elif onerror is not None:
            onerror(func, arg, exc)
            raise exc[0], (exc[1][0], exc[1][1] + ' removing '+arg)

Question by:LuckyLucks
    LVL 28

    Accepted Solution

    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.

    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):
        elif onerror is None:
            def onerror(*args):
        names = []
            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)
                mode = os.lstat(fullname).st_mode
            except os.error:
                mode = 0
            if stat.S_ISDIR(mode):
                rmtree(fullname, ignore_errors, onerror)
                except os.error, err:
                    onerror(os.remove, fullname, sys.exc_info())
        except os.error:
            onerror(os.rmdir, path, sys.exc_info())

    Open in new window

    LVL 28

    Expert Comment

    Possibly, you should think about Python 2.6.1 or at least Python 2.5.4 (i.e. the latest versions).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
    "The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now