【问题标题】:Skip directories in a search in Python在 Python 中的搜索中跳过目录
【发布时间】:2016-12-25 01:56:49
【问题描述】:

我正在运行以下代码,并且我想跳过 3 个具有各自名称的文件夹: 文件夹 1、文件夹 2、.repository。

但是,如果某些文件夹不存在,我会收到错误:

indentationerror: unindent 不匹配任何外部缩进级别

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

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)

--------更新:

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)

这里是输出:

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

最后更新 --------->

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)

我的输出:

    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

请帮忙,谢谢! :)

【问题讨论】:

  • 此外,即使文件夹存在,我最近也会收到此错误:IndentationError: expected an indented block
  • 此错误与您的问题无关。您确定文件中的缩进是正确的吗?
  • 该错误与您的问题无关,该错误仅与您的缩进有关
  • 我更新了输入/输出。一些帮助?谢谢

标签: python


【解决方案1】:

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)

改成:

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)

【讨论】:

  • 另外,不要在代码中混用制表符和空格。使用制表符或空格 - 首选空格 as per PEP8
  • 另外,PEP8 建议使用 4 个空格,这使得代码更加清晰。
  • 嗯,消息很清楚,它无法删除 x,因为 x 不在列表中
  • 看起来很清晰,但文件夹在那里。请不要添加中间目录,但无论如何我的输出看起来都在搜索它: ('Checking', '/home/dlopez/temp/test_folder/test1/test1a/pom.xml') ('以下文件包含用户代码模块: -------------> ', '/home/dlopez/temp/test_folder/test1/test1a/pom.xml') ('检查', '/home/dlopez/temp/test_folder/ test2/pom.xml') ('以下文件包含用户代码模块:-------------> ', '/home/dlopez/temp/test_folder/test2/pom.xml')
  • 请多给点小费?什么会失败?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多