如何在python中递归树时跳过.hg/.git/.svn目录

Rob*_*man 0 python directory os.walk

我有一个python脚本,我一直在拼凑(我的第一次python尝试之一).

该脚本会递归查找XCode项目文件的文件夹; 该脚本工作正常,但我想调整它以跳过任何.svn(或.hg或.git)文件夹,以便它不会尝试修改源存储库.

这是递归搜索的脚本

for root, dirnames, files in os.walk('.'):
    files = [f for f in files if re.search("project\.pbxproj", f)]
    for f in files:
        filename = os.path.join(root, f)
        print "Adjusting BaseSDK for %s" % (filename)
        ...
Run Code Online (Sandbox Code Playgroud)

如何排除存储库子树?

Epc*_*lon 5

正如S.Lott在他的评论中所说,这在文档中提到过os.walk.以下应该工作正常:

for root, dirs, files in os.walk("."):
    if ".hg" in dirs:
        dirs.remove(".hg")
    for f in files:
        print os.path.join(root, f)
Run Code Online (Sandbox Code Playgroud)