在Python中跳过搜索中的目录

在Python中跳过搜索中的目录,python,Python,我正在运行以下代码,我想跳过3个具有各自名称的文件夹: folder1、folder2、.存储库 但是,如果某些文件夹不存在,则会出现错误: 缩进错误:未缩进与任何外部缩进级别不匹配 如何搜索跳过该文件夹,即使它们不存在也不会得到任何错误?这是我的代码: import re import os from os.path import join comment=re.compile(r"<!--\s+\| Start of user code \(user defined modules\)

我正在运行以下代码,我想跳过3个具有各自名称的文件夹: folder1、folder2、.存储库

但是,如果某些文件夹不存在,则会出现错误:

缩进错误:未缩进与任何外部缩进级别不匹配

如何搜索跳过该文件夹,即使它们不存在也不会得到任何错误?这是我的代码:

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)
上次更新---------------->

重新导入
导入操作系统
从os.path导入联接
comment=re.compile(r“\s+”,re.MULTILINE)
tag=“”
对于os.walk(“/home/dlopez/temp/test_folder/”)中的根目录、目录和文件:
删除目录(“/home/temp/test\u folder/test1”)
删除目录(“/home/temp/test\u folder/test2”)
删除目录(“/home/temp/test\u folder/test3”)
如果文件中有“pom.xml”:
p=join(根,“pom.xml”)
打印(“检查”,p)
打开(p)作为f:
s=f.read()
如果在s中标记并注释.search:
打印(“以下文件包含用户代码模块:------------>”,p)
以及我的输出:

    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
python/home/temp/test\u文件夹/python\u脚本\u 5.py
回溯(最近一次呼叫最后一次):
文件“/home/dlopez/temp/test_folder/python_script_5.py”,第8行,在
删除目录(“/home/temp/test\u folder/test1”)
ValueError:list。删除(x):x不在列表中

请帮忙,谢谢!:)

if的缩进与当前缩进不匹配

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)
重新导入
导入操作系统
从os.path导入联接
comment=re.compile(r“\s+”,re.MULTILINE)
tag=“”
对于os.walk(“/home/dlopez/temp/”)中的根目录、目录和文件:
直接删除(“/home/dlopez/temp/test1”)
直接删除(“/home/dlopez/temp/test2”)
删除指令(“/home/dlopez/temp/test3”)
文件中的if“pom.xml:#if语句未与任何内容对齐
p=join(根,“pom.xml”)
打印(“检查”,p)
打开(p)作为f:
s=f.read()
如果在s中标记并注释.search:
打印(“以下文件包含用户代码模块:------------>”,p)
将其更改为:

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)
重新导入
导入操作系统
从os.path导入联接
comment=re.compile(r“\s+”,re.MULTILINE)
tag=“”
对于os.walk(“/home/dlopez/temp/”)中的根目录、目录和文件:
#跳过要循环列表的目录
对于在(“/home/dlopez/temp/test1”、“/home/dlopez/temp/test1”、“/home/dlopez/temp/test3”)中跳过的内容:
如果在目录中跳过:目录删除(跳过)
如果文件中有“pom.xml”:
p=join(根,“pom.xml”)
打印(“检查”,p)
打开(p)作为f:
s=f.read()
如果在s中标记并注释.search:
打印(“以下文件包含用户代码模块:------------>”,p)

此外,即使文件夹存在,我最近也会遇到此错误:缩进错误:预期为缩进块此错误与您的问题无关。您确定文件中的缩进是正确的吗?错误与您的问题无关,错误只与您的缩进有关可能与输入/输出更新的I重复有关。有什么帮助吗?谢谢。另外,不要在代码中混用制表符和空格。使用制表符或空格-首选空格。此外,PEP8建议使用4个空格,这使代码方式mroe清晰易读。消息很清楚,它无法删除x,因为x不在列表中。它看起来很清楚,但文件夹在那里。请不要添加中间目录,但无论如何,我的输出看起来都在搜索:('Checking'、'/home/dlopez/temp/test_folder/test1/test1a/pom.xml')('以下文件包含用户代码模块:------------>'、'/home/dlopez/temp/test_folder/test1/test1a/pom.xml')('Checking'、'/home/dlopez/temp/test_folder/test2/pom.xml'))(“以下文件包含用户代码模块:----------->”、“/home/dlopez/temp/test_folder/test2/pom.xml”)请提供更多提示?有什么可能失败?
    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
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)
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)