我有一个以前在 SVN 下的项目,但现在我将它移动到 Mercurial,所以我想清除所有的 .svn 文件。
手工做起来有点太大了,所以我决定写一个python脚本来做。但它不起作用。
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)
打印语句显示它只“考虑”名称以“.”开头的文件目录。但是,如果我取消注释 continue 语句,所有的 filedirs 都会被“考虑”。这是为什么?
或者是否已经存在一些其他实用程序来递归地反 SVN 化目录树?
请您参考如下方法:
至少你不应该为递归目录遍历重新发明轮子而只使用os.walk .
