如何在使用 os.walk 定义 ROOT_DIR 时从代码源分析中排除子目录和文件

问题描述

这是试图排除特定文件夹和文件被 pygments 分析。下面的方法给了我一个TypeError: 'str' object does not support item assignment。我明白 subdir一个字符串,但想不出解决它的方法

import os
from pygount import SourceAnalysis
from utils import ROOT_DIR #This is just an absolute path to root dir

class Counter:
    def __init__(self):
        self.code = 0
        self.docs = 0
        self.empty = 0

    def count(self):
        exclude = set(['.mypy_cache','.venv','.vscode','tests','.gitignore','poetry.lock','pyproject.toml'])
        for subdir,_,files in os.walk(ROOT_DIR / "TestProject"):
            subdir_ = subdir[:] = [s for s in subdir if s not in exclude]
            files_ = files[:] = [f for f in subdir if f not in exclude[subdir_]]
            for file in (f for f in files_ if f.endswith(".py")):
                    analysis = SourceAnalysis.from_file(f"{subdir_}/{file}","pygount",encoding="utf-8")
                    self.code += analysis.code_count
                    self.docs += analysis.documentation_count
                    self.empty += analysis.empty_count

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)