简体   繁体   中英

TypeError: 'str' object is not callable with input()

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()

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM