user2961008 user2961008 - 3 months ago 8
Python Question

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:

indentationerror: unindent does not match any outer indentation level

How can I search skipping that folders, and even if they are not present not get any error? Here my code:

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("."):
dirs.remove("folder1")
dirs.remove("folder2")
dirs.remove(".repo")
if "pom.xml" in files:
p=join(root, "pom.xml")
print("Checking",p)
with open(p) as f:
s=f.read()
if tag in s and comment.search(s):
print("The following file has been modified",p)


------UPDATE:

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/temp/"):
dirs.remove("/home/temp/test1")
dirs.remove("/home/temp/test2")
dirs.remove("/home/temp/test3")

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


And here the output:

python /home/temp/test_folder/python_script_4.py
File "/home/temp/test_folder/python_script_4.py", line 12
if "pom.xml" in files:
^
IndentationError: unexpected indent


LAST UPDATE --------->

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/test_folder/"):
dirs.remove("/home/temp/test_folder/test1")
dirs.remove("/home/temp/test_folder/test2")
dirs.remove("/home/temp/test_folder/test3")

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


And my Output:

python /home/temp/test_folder/python_script_5.py
Traceback (most recent call last):
File "/home/dlopez/temp/test_folder/python_script_5.py", line 8, in <module>
dirs.remove("/home/temp/test_folder/test1")
ValueError: list.remove(x): x not in list


Help please thanks! :)

Answer

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)
        with open(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 list
    for 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)
        with open(p) as f:
           s=f.read()
           if tag in s and comment.search(s):
              print("The following file contains user code modules:-------------> ",p)
Comments