python - 检查路径中是否存在某个文件夹

标签 python

我正在迭代文件夹并逐行读取文件以找到一些子字符串。

问题是我想在搜索过程中忽略某些文件夹(例如 binbuild)。

我尝试添加检查 - 但它仍然将其包含在搜索中

def step(ext, dirname, names):
    for name in names:
        if name.lower().endswith(".xml") or name.lower().endswith(".properties") or name.lower().endswith(".java"):
            path = os.path.join(dirname, name)
            if not "\bin\"" in path and not "\build" in path:
                with open(path, "r") as lines:
                    print "File: {}".format(path)
                    i = 0
                    for line in lines:
                        m = re.search(r"\{[^:]*:[^\}]*\}", line)
                        with open("search-result.txt", "a") as output:
                            if m is not None:
                                output.write("Path: {0}; \n    Line number: {1}; \n        String: {2}\n".format(path, i, m.group()))
                        i+=1

最佳答案

我怀疑您的条件失败,因为 \b 被解释为退格字符,而不是反斜杠字符后跟“b”字符。另外,看起来您在 bin 末尾转义了引号;这是故意的吗?

尝试转义反斜杠。

if not "\\bin\\" in path and not "\\build" in path:

关于python - 检查路径中是否存在某个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30102146/

相关文章:

python - map_reduce() *和* find() 在同一查询中

python - 为什么 PyObject_IsInstance 在我的示例代码中总是返回 0

python - 寻找不同的组合

python - 如何在不使用 json 库或没有任何其他库的情况下读取 json 文件到字典或列表中?

python - 包含的 urlconf 中没有任何模式 + django

python - Psycopg2:退出包含 try/except 的 'with' 语句时连接未提交

Python 拆分文本文件并保留换行符

python - 类函数的文本输出? -Tkinter

python - 如何在 python 中使用 pika (RabbitMQ) 向消费者添加多处理

python - 在用于 Python 开发的 Apache 2 中禁用缓存