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

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'

You should not be deleting those directories. The . directory is the current directory; the .. is the parent directory of the . directory. Every directory has those two pointers even when its empty. The filesystem will not let you remove those. The . and .. are essential to maintaining the directory tree by the file system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.