TypeError: forced to Unicode, need a string or buffer, NoneType detected

Currently, writing a function for a program and a single component is to find out if the same variables are used in the python file.

FUNCTION:

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​ 

When I used the function on its own, the function works correctly. However, when I access the program as a whole, I get the following error message:

 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 

I have absolutely no idea - where I'm wrong - any help will be very, very, very expensive!

thanks.

+5
source share
2 answers

python_filename set to None , which is not a valid argument for the open() function:

 >>> 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 None cannot be determined from the code you posted. The given trace assumes that the value appears in the sorted_list() function, I suggest you start looking for keys there:

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

However, this is just a hunch; you will need to trace all the code in this trace to see where exactly None is installed.

+8
source

Try to write like this:

 ssh.exec_command() stdin.flush() stdin.channel.shutdown_write() 

He should work

0
source

Source: https://habr.com/ru/post/1205497/


All Articles