0

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! :)

user2961008
  • 39
  • 1
  • 6

1 Answers1

0

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)
dfranca
  • 5,156
  • 2
  • 32
  • 60
  • 2
    In addition, do not mix tabs and spaces in your code. Use either tabs, or spaces - spaces are preferred [as per PEP8](https://www.python.org/dev/peps/pep-0008/#id17). – Burhan Khalid Aug 18 '16 at 08:26
  • Additionally, 4 spaces are recommended by PEP8, which makes code way mroe legible. – Ian Aug 18 '16 at 08:27
  • Well, the message is clear, it can't remove x because x is not in the list – dfranca Aug 18 '16 at 08:45
  • It looks clear, but the folders are there. Please not an intermediate directory added, but anyway my output looks searching on it: ('Checking', '/home/dlopez/temp/test_folder/test1/test1a/pom.xml') ('The following file contains user code modules:-------------> ', '/home/dlopez/temp/test_folder/test1/test1a/pom.xml') ('Checking', '/home/dlopez/temp/test_folder/test2/pom.xml') ('The following file contains user code modules:-------------> ', '/home/dlopez/temp/test_folder/test2/pom.xml') – user2961008 Aug 18 '16 at 08:57
  • some tip more please? what can be failing? – user2961008 Aug 18 '16 at 08:57
  • You're welcome, please vote/accept answer as it solved your question: http://stackoverflow.com/help/someone-answers – dfranca Aug 18 '16 at 09:12
  • Could you please advice here too? Is is possible to detect also ENTER between the line 3 and 4 of this input? I can detect only if there is some character more or less, i would like to detect also spaces: http://stackoverflow.com/questions/39007743/exact-string-search-in-xml-files/39007836#39007836 – user2961008 Aug 18 '16 at 09:35