Python: problem with tiny script to delete files
- by Rosarch
I have a project that used to be under SVN, but now I'm moving it to Mercurial, so I want to clear out all the .svn files.
It's a little too big to do it by hand, so I decided to write a python script to do it. But it isn't working.
def cleandir(dir_path):
print "Cleaning %s\n" % dir_path
toDelete = []
files = os.listdir(dir_path)
for filedir in files:
print "considering %s" % filedir
# continue
if filedir == '.' or filedir == '..':
print "skipping %s" % filedir
continue
path = dir_path + os.sep + filedir
if os.path.isdir(path):
cleandir(path)
else:
print "not dir: %s" % path
if 'svn' in filedir:
toDelete.append(path)
print "Files to be deleted:"
for candidate in toDelete:
print candidate
print "Delete all? [y|n]"
choice = raw_input()
if choice == 'y':
for filedir in toDelete:
if os.path.isdir(filedir):
os.rmdir(filedir)
else:
os.unlink(filedir)
exit()
if __name__ == "__main__":
cleandir(dir)
The print statements show that it's only "considering" the filedirs whose names start with ".". However, if I uncomment the continue statement, all the filedirs are "considered". Why is this?
Or is there some other utility that already exists to recursively de-SVN-ify a directory tree?