1

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 adapt 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?

3
  • 9
    Did you look at the os.walk documentation? docs.python.org/library/os.html#os.walk. The exact solution is in the documentation. dirnames.remove(".svn")
    – S.Lott
    Apr 29, 2011 at 22:17
  • Just came across here as i wanted to delete all .svn files in a project, the command svn export just does this without the need for a script (in hg & git there is just one folder in the root of the project you have to delete).
    – moritz
    Apr 29, 2011 at 23:06
  • @S.Lott Thank you for pointing out the spot in documentation; not sure why I missed it before. Apr 30, 2011 at 19:57

2 Answers 2

6

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

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)
1
  • .hg, .git, .svn are folders. Also see the comment in question above.
    – manojlds
    Apr 29, 2011 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.