TypeError: coercing to Unicode, need string or buffer, NoneType found
Asked Answered
R

2

5

Currently writing a function for a program and one component is to search whether a single variables are being used within a 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 by itself - the function works properly. However when i call upon 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 am going wrong - any help would be very, very, very cherished!!!

thanks.

Rialto answered 25/10, 2014 at 13:39 Comment(1)
C
11

python_filename is 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 is None cannot be determined from the code you posted. The traceback you supplied suggests the value originates in the sorted_list() function, I suggest you start looking there for clues:

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

However, that is just a guess; you'll have to trace through all the code in that traceback to see where exactly the None is first set.

Charron answered 25/10, 2014 at 13:49 Comment(2)
How do you know that python_filename is set to none??Rialto
The exception message tells you. open() expected a string, but was given an object of type NoneType instead. There is only ever one object that fits that description: None.Charron
D
0

Try writing this way:

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

It should work

Durning answered 21/12, 2015 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.