Skip to content Skip to sidebar Skip to footer

Skip Directories In A Search In Python

Im running the following code, and i want to skip 3 folders with respective names: folder1, folder2, .repository. However if some of the folders is not present i get the error: ind

Solution 1:

Your indentation of the if doesn't match the current indentation

import re
import os
from os.path import join
comment=re.compile(r"<!--\s+\| Start of user code \(user defined modules\)\s+\|-->\s+<!--\s+\| End of user code\s+\|-->", re.MULTILINE)
tag="<module>"for root, dirs, files in os.walk("/home/dlopez/temp/"):
 dirs.remove("/home/dlopez/temp/test1")
 dirs.remove("/home/dlopez/temp/test2")
 dirs.remove("/home/dlopez/temp/test3")

       if"pom.xml"in files:  # The if statement is not aligned to anything
        p=join(root, "pom.xml") 
        print("Checking",p)
        withopen(p) as f:
            s=f.read()
        if tag in s and comment.search(s):
            print("The following file contains user code modules:-------------> ",p)

Change it to:

import re
import os
from os.path import join
comment=re.compile(r"<!--\s+\| Start of user code \(user defined modules\)\s+\|-->\s+<!--\s+\| End of user code\s+\|-->", re.MULTILINE)
tag="<module>"for root, dirs, files in os.walk("/home/dlopez/temp/"):
    # Skip the dirs you want looping a listfor skipped in ("/home/dlopez/temp/test1", "/home/dlopez/temp/test1", "/home/dlopez/temp/test3"):
        if skipped in dirs: dirs.remove(skipped)

    if"pom.xml"in files:
        p=join(root, "pom.xml") 
        print("Checking",p)
        withopen(p) as f:
           s=f.read()
           if tag in s and comment.search(s):
              print("The following file contains user code modules:-------------> ",p)

Post a Comment for "Skip Directories In A Search In Python"