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() 
1
  • 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 Commented May 13, 2019 at 19:24

4 Answers 4

7

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.

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

1 Comment

Take away message is, don't name variables similar to function names.
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 

Comments

0

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

2 Comments

First need to change the variable name input to some other variable because python interpreter is confused, and then restart the kernel
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.
-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) 

1 Comment

No, that is not the issue. They have trampled over the input builtin as the error conveys