recursively remove all the directories and files which begin with '.'

Discussion in 'Python' started by albert kao, May 14, 2010.

  1. albert kao

    albert kao Guest

    How do I recursively remove all the directories and files which begin
    with '.'?
    My test program rmdir.py does not do the job yet.
    Please help.
    Code:
    #!c:/Python31/python.exe -u
    import os
    from shutil import *
    
    root = "C:\\test\\com.comp.hw.prod.proj.war\\bin"
    
    for curdir, dirs, files in os.walk(root):
      print (curdir)
      print (dirs)
      for d in dirs:
        print ("d " + d)
        if d.startswith('.'):
          print ("dotd " + os.path.join(curdir, d))
          rmtree(os.path.join(curdir, d))
    
    C:\python>rmdir.py
    C:\test\com.comp.hw.prod.proj.war\bin
    ['.svn', 'com']
    d .svn
    dotd C:\test\com.comp.hw.prod.proj.war\bin\.svn
    Traceback (most recent call last):
    File "C:\python\rmdir.py", line 14, in <module>
    rmtree(os.path.join(curdir, d))
    File "C:\Python31\lib\shutil.py", line 235, in rmtree
    onerror(os.remove, fullname, sys.exc_info())
    File "C:\Python31\lib\shutil.py", line 233, in rmtree
    os.remove(fullname)
    WindowsError: [Error 5] Access is denied: 'C:\\test\
    \com.comp.hw.prod.proj.war\\bin\\.svn\\entries'
    albert kao, May 14, 2010
    #1
    1. Advertising

  2. albert kao

    J Guest

    Re: recursively remove all the directories and files which begin with'.'

    On Fri, May 14, 2010 at 10:53, albert kao <> wrote:
    >
    > C:\python>rmdir.py
    > C:\test\com.comp.hw.prod.proj.war\bin
    > ['.svn', 'com']
    > d .svn
    > dotd C:\test\com.comp.hw.prod.proj.war\bin\.svn
    > Traceback (most recent call last):
    >  File "C:\python\rmdir.py", line 14, in <module>
    >    rmtree(os.path.join(curdir, d))
    >  File "C:\Python31\lib\shutil.py", line 235, in rmtree
    >    onerror(os.remove, fullname, sys.exc_info())
    >  File "C:\Python31\lib\shutil.py", line 233, in rmtree
    >    os.remove(fullname)
    > WindowsError: [Error 5] Access is denied: 'C:\\test\
    > \com.comp.hw.prod.proj.war\\bin\\.svn\\entries'
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    You don't have permissions to remove the subdir or file entries in the
    ..svn directory...

    Maybe that file is still open, or still has a lock attached to it?
    J, May 14, 2010
    #2
    1. Advertising

  3. albert kao

    albert kao Guest

    Re: recursively remove all the directories and files which begin with'.'

    On May 14, 11:01 am, J <> wrote:
    > On Fri, May 14, 2010 at 10:53, albert kao <> wrote:
    >
    > > C:\python>rmdir.py
    > > C:\test\com.comp.hw.prod.proj.war\bin
    > > ['.svn', 'com']
    > > d .svn
    > > dotd C:\test\com.comp.hw.prod.proj.war\bin\.svn
    > > Traceback (most recent call last):
    > >  File "C:\python\rmdir.py", line 14, in <module>
    > >    rmtree(os.path.join(curdir, d))
    > >  File "C:\Python31\lib\shutil.py", line 235, in rmtree
    > >    onerror(os.remove, fullname, sys.exc_info())
    > >  File "C:\Python31\lib\shutil.py", line 233, in rmtree
    > >    os.remove(fullname)
    > > WindowsError: [Error 5] Access is denied: 'C:\\test\
    > > \com.comp.hw.prod.proj.war\\bin\\.svn\\entries'

    >
    > > --
    > >http://mail.python.org/mailman/listinfo/python-list

    >
    > You don't have permissions to remove the subdir or file entries in the
    > .svn directory...
    >
    > Maybe that file is still open, or still has a lock attached to it?


    I reboot my windows computer and run this script as administrator.
    Do my script has a bug?
    albert kao, May 14, 2010
    #3
  4. albert kao

    Sean DiZazzo Guest

    Re: recursively remove all the directories and files which begin with'.'

    On May 14, 8:27 am, albert kao <> wrote:
    > On May 14, 11:01 am, J <> wrote:
    >
    >
    >
    > > On Fri, May 14, 2010 at 10:53, albert kao <> wrote:

    >
    > > > C:\python>rmdir.py
    > > > C:\test\com.comp.hw.prod.proj.war\bin
    > > > ['.svn', 'com']
    > > > d .svn
    > > > dotd C:\test\com.comp.hw.prod.proj.war\bin\.svn
    > > > Traceback (most recent call last):
    > > >  File "C:\python\rmdir.py", line 14, in <module>
    > > >    rmtree(os.path.join(curdir, d))
    > > >  File "C:\Python31\lib\shutil.py", line 235, in rmtree
    > > >    onerror(os.remove, fullname, sys.exc_info())
    > > >  File "C:\Python31\lib\shutil.py", line 233, in rmtree
    > > >    os.remove(fullname)
    > > > WindowsError: [Error 5] Access is denied: 'C:\\test\
    > > > \com.comp.hw.prod.proj.war\\bin\\.svn\\entries'

    >
    > > > --
    > > >http://mail.python.org/mailman/listinfo/python-list

    >
    > > You don't have permissions to remove the subdir or file entries in the
    > > .svn directory...

    >
    > > Maybe that file is still open, or still has a lock attached to it?

    >
    > I reboot my windows computer and run this script as administrator.
    > Do my script has a bug?


    Are the directory or files marked as read only?

    See this recipe and the comment from Chad Stryker:

    http://code.activestate.com/recipes/193736-clean-up-a-directory-tree/

    "Although it is true you can use shutil.rmtree() in many cases, there
    are some cases where it does not work. For example, files that are
    marked read-only under Windows cannot be deleted by shutil.rmtree().
    By importing the win32api and win32con modules from PyWin32 and adding
    line like "win32api.SetFileAttributes(path,
    win32con.FILE_ATTRIBUTE_NORMAL" to the rmgeneric() function, this
    obstacle can be overcome."

    It might not be your problem, but if it is, this had me stumped for a
    few weeks before I found this comment!

    ~Sean
    Sean DiZazzo, May 16, 2010
    #4
  5. Re: recursively remove all the directories and files which beginwith '.'

    On 16-5-2010 19:41, Sean DiZazzo wrote:
    > On May 14, 8:27 am, albert kao<> wrote:
    >> On May 14, 11:01 am, J<> wrote:
    >>
    >>
    >>
    >>> On Fri, May 14, 2010 at 10:53, albert kao<> wrote:

    >>
    >>>> C:\python>rmdir.py
    >>>> C:\test\com.comp.hw.prod.proj.war\bin
    >>>> ['.svn', 'com']
    >>>> d .svn
    >>>> dotd C:\test\com.comp.hw.prod.proj.war\bin\.svn
    >>>> Traceback (most recent call last):
    >>>> File "C:\python\rmdir.py", line 14, in<module>
    >>>> rmtree(os.path.join(curdir, d))
    >>>> File "C:\Python31\lib\shutil.py", line 235, in rmtree
    >>>> onerror(os.remove, fullname, sys.exc_info())
    >>>> File "C:\Python31\lib\shutil.py", line 233, in rmtree
    >>>> os.remove(fullname)
    >>>> WindowsError: [Error 5] Access is denied: 'C:\\test\
    >>>> \com.comp.hw.prod.proj.war\\bin\\.svn\\entries'

    >>
    >>>> --
    >>>> http://mail.python.org/mailman/listinfo/python-list

    >>
    >>> You don't have permissions to remove the subdir or file entries in the
    >>> .svn directory...

    >>
    >>> Maybe that file is still open, or still has a lock attached to it?

    >>
    >> I reboot my windows computer and run this script as administrator.
    >> Do my script has a bug?

    >
    > Are the directory or files marked as read only?
    >
    > See this recipe and the comment from Chad Stryker:
    >
    > http://code.activestate.com/recipes/193736-clean-up-a-directory-tree/
    >
    > "Although it is true you can use shutil.rmtree() in many cases, there
    > are some cases where it does not work. For example, files that are
    > marked read-only under Windows cannot be deleted by shutil.rmtree().
    > By importing the win32api and win32con modules from PyWin32 and adding
    > line like "win32api.SetFileAttributes(path,
    > win32con.FILE_ATTRIBUTE_NORMAL" to the rmgeneric() function, this
    > obstacle can be overcome."
    >
    > It might not be your problem, but if it is, this had me stumped for a
    > few weeks before I found this comment!
    >
    > ~Sean


    You should be able to do this with os.chmod as well (no extra modules
    required). I'm not sure what the mode should be though. Perhaps 0777
    does the trick.

    -irmen
    Irmen de Jong, May 16, 2010
    #5
  6. albert kao

    Walter Wefft Guest

    Re: recursively remove all the directories and files which beginwith '.'

    Irmen de Jong wrote:
    > On 16-5-2010 19:41, Sean DiZazzo wrote:
    >> On May 14, 8:27 am, albert kao<> wrote:
    >>> On May 14, 11:01 am, J<> wrote:
    >>>
    >>>
    >>>
    >>>> On Fri, May 14, 2010 at 10:53, albert kao<> wrote:
    >>>
    >>>>> C:\python>rmdir.py
    >>>>> C:\test\com.comp.hw.prod.proj.war\bin
    >>>>> ['.svn', 'com']
    >>>>> d .svn
    >>>>> dotd C:\test\com.comp.hw.prod.proj.war\bin\.svn
    >>>>> Traceback (most recent call last):
    >>>>> File "C:\python\rmdir.py", line 14, in<module>
    >>>>> rmtree(os.path.join(curdir, d))
    >>>>> File "C:\Python31\lib\shutil.py", line 235, in rmtree
    >>>>> onerror(os.remove, fullname, sys.exc_info())
    >>>>> File "C:\Python31\lib\shutil.py", line 233, in rmtree
    >>>>> os.remove(fullname)
    >>>>> WindowsError: [Error 5] Access is denied: 'C:\\test\
    >>>>> \com.comp.hw.prod.proj.war\\bin\\.svn\\entries'
    >>>
    >>>>> --
    >>>>> http://mail.python.org/mailman/listinfo/python-list
    >>>
    >>>> You don't have permissions to remove the subdir or file entries in the
    >>>> .svn directory...
    >>>
    >>>> Maybe that file is still open, or still has a lock attached to it?
    >>>
    >>> I reboot my windows computer and run this script as administrator.
    >>> Do my script has a bug?

    >>
    >> Are the directory or files marked as read only?
    >>
    >> See this recipe and the comment from Chad Stryker:
    >>
    >> http://code.activestate.com/recipes/193736-clean-up-a-directory-tree/
    >>
    >> "Although it is true you can use shutil.rmtree() in many cases, there
    >> are some cases where it does not work. For example, files that are
    >> marked read-only under Windows cannot be deleted by shutil.rmtree().
    >> By importing the win32api and win32con modules from PyWin32 and adding
    >> line like "win32api.SetFileAttributes(path,
    >> win32con.FILE_ATTRIBUTE_NORMAL" to the rmgeneric() function, this
    >> obstacle can be overcome."
    >>
    >> It might not be your problem, but if it is, this had me stumped for a
    >> few weeks before I found this comment!
    >>
    >> ~Sean

    >
    > You should be able to do this with os.chmod as well (no extra modules
    > required). I'm not sure what the mode should be though. Perhaps 0777
    > does the trick.
    >
    > -irmen
    >


    def make_readable(fpath):
    '''
    On windows, this will make a read-only file readable.
    '''
    mode = os.stat(fpath)[stat.ST_MODE] | stat.S_IREAD | stat.S_IWRITE
    chmod(fpath, mode)
    Walter Wefft, May 16, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken
    Replies:
    1
    Views:
    3,736
    Roedy Green
    Dec 2, 2005
  2. rbt
    Replies:
    5
    Views:
    8,973
  3. bahoo
    Replies:
    2
    Views:
    309
    Eli Criffield
    Apr 9, 2007
  4. albert kao
    Replies:
    3
    Views:
    610
    Tim Chase
    May 14, 2010
  5. Adam Petrie
    Replies:
    8
    Views:
    305
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page