[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1288
  • Last Modified:

permission denied when using rmtree

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)

0
LuckyLucks
Asked:
LuckyLucks
  • 2
1 Solution
 
peprCommented:
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

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now