2

I have the following code, which is supposed to ask the user 2 file names. I get an error with the input() in the second function but not in the first, I don't understand... Here is the error :

output = getOutputFile() File "splitRAW.py", line 22, in getOutputFile fileName = input("\t=> ") TypeError: 'str' object is not callable

# Loops until an existing file has been found
def getInputFile():
    print("Which file do you want to split ?")
    fileName = input("\t=> ")
    while 1:
        try:
            file = open(fileName, "r")
            print("Existing file, let's continue !")
            return(fileName)
        except IOError:
            print("No such existing file...")
            print("Which file do you want to split ?")
            fileName = input("\t=> ")

# Gets an output file from user
def getOutputFile():
    print("What name for the output file ?")
    fileName = input("\t=> ")

And here is my main() :

if __name__ == "__main__":
    input = getInputFile()
    output = getOutputFile()
  • 3
    Somewhere in your code, you've defined `input = something_here`. Depending on your IDE, it may have have been in a different file or within the console – roganjosh May 13 '19 at 19:24
  • That's it, thanks !! –  May 13 '19 at 19:25

4 Answers4

6

The problem is when you say input = getInputFile().

More specifically:

  1. The program enters the getInputFile() function, and input hasn't been assigned yet. That means the Python interpreter will use the built-in input, as you intended.
  2. You return filename and get out of getInputFile(). The interpreter now overwrites the name input to be that string.
  3. getOutputFile() now tries to use input, but it's been replaced with your file name string. You can't call a string, so the interpreter tells you that and throws an error.

Try replacing input = getInputFile() with some other variable, like fileIn = getInputFile().

Also, your getOutputFile() is not returning anything, so your output variable will just have None in it.

Adeola Bannis
  • 341
  • 4
  • 6
1

You may be overriding the input name with something else.

If you need to reinitialize the input function in a notebook:

from builtin import input
Bazzert
  • 77
  • 2
  • 7
0

Next time just "RESTART YOUR KERNEL" TypeError: 'str' object is not callable - restart kernel and its gone. You're good to go.

  • First need to change the variable name input to some other variable because python interpreter is confused, and then restart the kernel – mAge Apr 27 '21 at 05:00
  • Restarting kernel resolves my problem. In my case, I used jupyter notebook in VS code with simple code `a = input()`, got *TypeError: 'str' object is not callable*. – iroiroys Jan 03 '23 at 03:35
-3

Depending on what version of python you're using:

Python 2:

var = raw_input("Please enter something: ")
print "you entered", var

Or for Python 3:

var = input("Please enter something: ")
print("You entered: " + var)