3

I have written following program for my project purpose.

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

While running this code I am getting following error:

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

Can anyone help me to solve this?

3
  • Your traceback doesn't match the code you posted; we can see a similar line that has the same problem, but do post actual errors thrown by the code you posted. Commented Apr 2, 2014 at 17:01
  • Use different variable instead of str. Commented Apr 2, 2014 at 17:01
  • 1
    str = f.read() shows that you are obviously not interested in keeping access to the builtin str function/type. That's ok, but you should be aware that you should stand by that. (SCNR) Commented Apr 2, 2014 at 17:03

3 Answers 3

8

You assigned the file contents to str:

str = f.read()

This masks the built-in function.

Don't use str as a local name; pick something else.

Sign up to request clarification or add additional context in comments.

Comments

3

In the following line, the code overwrite builtin function str with a string object.

str = f.read()

Use different name to prevent that.


>>> str(1)
'1'
>>> str = 'string object'
>>> str(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

Comments

1

str is a reserved name to handle string values . so you can use it as a local variable name.
pick something else.
do

str_value = f.read()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.