Navin146
Navin146

Reputation: 11

TypeError: 'str' object is not callable

I am stuck with these 2 errors in Python 3.3.2:

import os
path="D:\\Data\\MDF Testing\\MDF 4 -Bangalore\\Bangalore Testing"
os.chdir(path)

for file in os.listdir("."):
    if file.endswith(".doc"):
        print('FileName is ', file)


def testcasenames(file):
    nlines = 0
    lookup="Test procedures"
    procnames=[]
    temp=[]
    '''Open a doc file and try to get the names of the various test procedures:'''
    f = open(file, 'r')
    for line in f:
        val=int(nlines)+1
        if (lookup in line):
            val1=int(nlines)
        elif(line(int(val))!=" ") and line(int(val1))==lookup):
            temp=line.split('.')
            procnames.append(temp[1])
        else:
            continue
    return procnames

filename="MDF_Bng_Test.doc"
testcasenames(filename)

Traceback (most recent call last):
  File "D:/Data/Python files/MS_Word_Python.py", line 34, in <module>
    testcasenames(filename)
  File "D:/Data/Python files/MS_Word_Python.py", line 25, in testcasenames
    elif(line(val)!=" " and line(val1)==lookup):
TypeError: 'str' object is not callable

The idea is to only get the test procedure names after I get the Section "Test procedures" while looping in the Test document file (MDF_Bng_Test.doc) and after that I copy all the test procedure names (T_Proc_2.1,S_Proc_2.2...)coming under it.

Ex:

    1.1.1 Test objectives
       1.Obj 1.1
       2.Obj 1.2
       3.Obj 1.3
       4.Obj 1.4
  **2.1.1 Test procedures
       1.T_Proc_2.1
       2.S_Proc_2.2
       3.M_Proc_2.3
       4.N_Proc_2.4**
    3.1.1 Test References
       1.Refer_3.1
       2.Refer_3.2
       3.Refer_3.3

Upvotes: 0

Views: 2276

Answers (2)

Paulo Bu
Paulo Bu

Reputation: 29804

The problem is in this line:

elif(line(int(val))!=" ") and line(int(val1))==lookup):

If you are trying to index the string, Python uses square brackets notation ([]) to accomplish it, it would be like this:

elif(line[int(val)]!=" ") and line[int(val1)]==lookup):

Another suggestion, parenthesis wrapping if..else statements in Python are optional and normally the code looks better without them:

elif line[int(val)]!=" " and line[int(val1)]==lookup:

Hope this helps!

Upvotes: 1

thefourtheye
thefourtheye

Reputation: 239653

when you use () with line, it thinks that line is a function which actually is not. What you actually need to use is [] notation

line[int(val)]!=" " and line[int(val1)]==lookup

Upvotes: 1

Related Questions