Python input() TypeError: '_io.TextIOWrapper' object is not callable

在写一个复制文件内容的demo时,shell报了一个错误



我的代码是:

from sys import argv
from os.path import exists

script, from_file, to_file = argv

input = open(from_file)
inData = input.read()

print(f"The input file is {len(inData)} bytes long.")
print("Does the output file exist?", exists(to_file))
input()

output = open(to_file, 'w')
output.write(inData)

print("Done!")
input.close()
output.close()

搜了“TypeError: '_io.TextIOWrapper' object is not callable”报错信息,Google了一番后,看到有人说是上面调用过了。仔细看下代码,果然上面有一个变量,命名为“input”了。
我们把这个变量名改下:

#......部分无关代码省略
input_file = open(from_file)
inData = input_file.read()

print(f"The input file is {len(inData)} bytes long.")
print("Does the output file exist?", exists(to_file))
input()

output = open(to_file, 'w')
output.write(inData)

print("Done!")
input_file.close()
output.close()

再次运行,就没有报错了。input()这个方法本身是可以多次调用的,但是给变量命名的时候要注意区分,否则会报错。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。