TypeError:强制转换为 Unicode,需要字符串或缓冲区,未找到 NoneType

2024-01-02

目前正在为程序和一个组件编写一个函数,用于搜索 python 文件中是否正在使用单个变量。

功能:

def SINGLE_CHAR_VAR(python_filename):
    file = open(python_filename)
    lines = [0]
    SINGLE_CHAR_VAR = []
    for line in file:
        stripped = line.strip('\n\r')
        lines.append(stripped)

    from utils import vars_indents
    variable_list = (vars_indents(python_filename))[0]
    for i in range(1, len(variable_list)):
        if len(variable_list[i][0][0]) == 1:
            SINGLE_CHAR_VAR.append(['SINGLE_CHAR_VAR', i, variable_list[i][0][1], variable_list[i][0][0], lines[i]])      
    return SINGLE_CHAR_VAR​

当我单独使用该函数时 - 该函数工作正常。 但是,当我调用整个程序时 - 我收到以下错误消息:

Traceback (most recent call last):
  File "<web session>", line 1, in <module>
  File "lint_2.py", line 141, in lint
    sorted_error_list = sorted_list(list_of_file_errors)
  File "lint_2.py", line 84, in sorted_list
    error_list = total_error_list(python_filename)
  File "lint_2.py", line 65, in total_error_list
    single_char_var_list = SINGLE_CHAR_VAR(python_filename)
  File "lint_2.py", line 33, in SINGLE_CHAR_VAR
    file = open(python_filename)
TypeError: coercing to Unicode: need string or buffer, NoneType found

我完全不知道 - 我哪里出错了 - 任何帮助将非常非常非常珍惜!

thanks.


python_filename被设定为None,这不是一个有效的论据open()功能:

>>> open(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, NoneType found

Why python_filename is None无法从您发布的代码确定。您提供的回溯表明该值源自sorted_list()函数,我建议你开始在那里寻找线索:

  File "lint_2.py", line 84, in sorted_list
    error_list = total_error_list(python_filename)

然而,这只是一个猜测;您必须跟踪回溯中的所有代码才能查看到底在哪里None是第一组。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TypeError:强制转换为 Unicode,需要字符串或缓冲区,未找到 NoneType 的相关文章

随机推荐