0

I trying to open file and met some problem:

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

Here is the code example:

a = open(fname, "rb").read(255)

Whats wrong with the code?

4
  • What is fname? Can you show some more code. The one preceeding this Commented Jun 27, 2014 at 12:59
  • My guess is that fname is None Commented Jun 27, 2014 at 13:00
  • You think that fname contains a string but in fact, the way you have structured your previous code, fname is somehow being assigned None. That is why we are asking to see some more code. Commented Jun 27, 2014 at 13:01
  • Yes, I'm checked it. Actually, fname really contain the None Commented Jun 27, 2014 at 13:06

1 Answer 1

5

fname is None, not a string:

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

You'll have to fix how you set fname or explicitly guard against it being None.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.