TypeError: coercing to Unicode: need string or buffer, file found?
Question: I am having problems with a very early prototype of a simple chat bot that
will use the conversation that it is having to add to a database of responses
that it can use later.

    
    
      import sys,time,random, os.path
    
    typing_speed = 50 #wpm
    def slow_type(t):
        for l in t:
            sys.stdout.write(l)
            sys.stdout.flush()
            time.sleep(random.random()*10.0/typing_speed)
        print ''
    
    slow_type("Hello! My name is TUTAI, or Turing Test Artificial Intelligance")
    
    slow_type("Currently I am in training, so my features arent fully complete.")
    
    slow_type("If you say something I don't understand yet, I will repeat it back to you in order for me to learn and build a databace of responces!")
    
    talk = raw_input()
    
    talk = talk + ".txt"
    
    existance = True
    
    try:
        talk = open(talk, "r")
    
    except:
        existance = False
        talk.close()
    
    if existance == True:
        talkBack = open(talk, "r")
        print talkBack.read()
    

However, when I run the program, I get this response(yes I checked that the
file exists).

    
    
    Hello! My name is TUTAI, or Turing Test Artificial Intelligence
    Currently I am in training, so my features aren't fully complete.
    If you say something I don't understand yet, I will repeat it back to you in order for me to learn and build a database of responses!
    (I type)Hello
    
    Traceback (most recent call last):
      File "H:\TUTAI\firstPythonScript.py", line 31, in <module>
        talkBack = open(talk, "r")
    TypeError: coercing to Unicode: need string or buffer, file found
    

Thank You! (I know I imported a bunch of stuff I don't need. Pls don't mention
that.)


Answer: My guess is that by that point in the program, the 'talk' variable is a file
handle, not a string. I would discourage the kind of variable re-use seen here
as it leads to exactly the problem you are having.

Your code progression:

    
    
    talk = raw_input() #string
    talk = talk + '.txt' #string
    talk = open(talk, 'r') #file handle
    talkBack = open(talk, "r") #error, talk is still file handle
    

