美文网首页
Python input() TypeError: '_io.T

Python input() TypeError: '_io.T

作者: Sakumi | 来源:发表于2019-05-31 10:34 被阅读0次

在写一个复制文件内容的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()这个方法本身是可以多次调用的,但是给变量命名的时候要注意区分,否则会报错。

相关文章

网友评论

      本文标题:Python input() TypeError: '_io.T

      本文链接:https://www.haomeiwen.com/subject/owtwtctx.html