在尝试制作一个简单的文本文件阅读器/编辑器时,我发现每次尝试立即为变量赋值时,每次使用输入函数时都会出错。
文件“。\ textFileHandler.py”,第3行,在 input2 = input(“文本文件的名称是什么?”) TypeError:“ str”对象不可调用
input=input("1 - Open an existing file\n2 - Create a new file\n3 - Add elements to an existing file")
if input=="1":
input2 = input("What is the name of the text file?")
file=open(input2+".txt","r")
textContent=file.readlines()
print(textContent)
file.close()
elif input=="2":
input2 = input("How would you like to name your new file? ")
file = open(input2+".txt","w")
string = input("Input text: ")
file.write(string)
file.close()
elif input=="3":
input2 = input("What is the name of the text file? ")
file = open(input2+".txt","a")
string = input("Input text: ")
file.write(string)
file.close()
input("Press any key to exit.")
答案 0 :(得分:0)
input=input("1 - Open an existing file\n2 - Create a new file\n3 - Add elements to an existing file")
使用Python屏蔽内置函数是不幸的陷阱。解决此问题的一种有效方法是使用编辑器,以突出显示内置名称的方式与其他名称不同。