开发者

How to skip .hg / .git / .svn directories while recursing tree in python

I have a python script which I have been piecing together (one of my first python forays).

The script recurses a folder looking for XCode project files; the script works fine, but I would like to a开发者_开发知识库dapt it to skip any .svn (or .hg or .git) folders so that it isn't trying to modify source repositories.

Here is the script for the recursive search

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)
        ...

How can I exclude the repository sub-trees?


As S.Lott says in his comment, this is mentioned in the documentation for os.walk. The following should work fine:

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


Before handling the file, you can check if the first character in the filename begins with a ".", if it does, continue to the next item in the loop.

for root, dirnames, files in os.walk('.'):
    files = [f for f in files if re.search("project\.pbxproj", f)]
    for f in files:
        ### EDIT START
        if f[0] == ".":
            continue
        ### EDIT FINISH

        filename = os.path.join(root, f)
        print "Adjusting BaseSDK for %s" % (filename)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜