TypeError:'str'对象在Python中不可调用

Mad*_*dan 3 python python-2.7

我已经为我的项目目的编写了以下程序.

import glob
import os
path = "/home/madhusudan/1/*.txt"
files = glob.glob(path)

s1 = "<add>\n<doc>\n\t<field name = \"id\">"
s2 = "</field>\n"
s3 = "<field name = \"features\">"
s4 = "</doc>\n</add>"

i = 150
for file in files:
    f = open(file,"r")
    str = f.read()
    file1 = "/home/madhusudan/2/"+os.path.splitext(os.path.basename(file))[0] + ".xml"
    f1 = open(file1,"w")
    content = s1 + str(i) + s2 + s3 + f.read() + s2 + s4 
    f1.write(content)
    i = i + 1
Run Code Online (Sandbox Code Playgroud)

运行此代码时,我收到以下错误:

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    id1 = str(i)
TypeError: 'str' object is not callable
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决这个问题吗?

Mar*_*ers 8

您已将文件内容分配给str:

str = f.read()
Run Code Online (Sandbox Code Playgroud)

这掩盖了内置功能.

不要str用作本地名称; 选择其他东西.