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()這個方法本身是可以多次調用的,但是給變量命名的時候要注意區(qū)分,否則會報錯。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容