Skip to main content
 首页 » 编程设计

Python: 'str' 对象不可调用

2025年05月04日9haluo1

我有以下代码

f = open('BigTestFile','w'); 
str = '0123456789' 
for i in range(100000000): 
    if i % 1000000 == 0: 
        print(str(i / 1000000) + ' % done') 
    f.write(str) 
f.close() 

当我运行它时,我得到了这个TypeError:

Traceback (most recent call last): 
  File "gen_big_file.py", line 8, in <module> 
        print(str(i / 1000000) + ' % done') 
TypeError: 'str' object is not callable 

这是为什么呢?如何修复?

请您参考如下方法:

将变量称为 str 以外的名称。 它正在跟踪 str built in function.