BlackFox89
BlackFox89

Reputation: 1

Can't run a python script ('str' object is not callable)

I am trying to make a python program, that will help me read notifications log easily. Here's the code:-

location=open("/home/pika/.cache/xfce4/notifyd/log","rt")
data=location.read()
l_data=list(data) #Contents of log file is in string now, in data variable
x=data.count('app_name')
def rm(start,stop,p_name):
    for x in range(start,stop+1):
        print(x)
        n=p_name(x)
        m=l_data.remove(n)
        print(m)        
    data=''
    data=data.join(l_data)
for i in range(0,x):    
    #Time of notification
    t_start=data.index('[')
    t_end=data.index(']')
    t=data[t_start:t_end+1]
    print(t)
    print('\n')
    rm(t_start,t_end,t)
    #Name of the application
    name_start=data.index('app_name')
    name_end=data.index('summary')
    name=data[name_start:name_end-1]
    print(name)
    print('\n')
    rm(name_start,name_end,name)
    #Heading of notification
    head_start=data.index('body')
    head_end=data.index('app_icon')
    head=data[head_start:head_end-1]
    print(head)
    print('\n')
    rm(head_start,head_end,head)
    print('-----------------------------------------------------------')

But, it is giving me the following error:-

[2020-07-23T16:24:43]


0
Traceback (most recent call last):
  File "New File.py", line 20, in <module>
    rm(t_start,t_end,t)
  File "New File.py", line 8, in rm
    n=p_name(x)
TypeError: 'str' object is not callable

Any idea what's the issue? (p.s. i am new to programming, sorry for messy code)

Upvotes: 0

Views: 114

Answers (2)

You called the function rm() with last parameter p_name as a string.

t=data[t_start:t_end+1] # this is a string
rm(t_start,t_end, t) # t is a string

Inside the function you assign n = p_name(x) which causes the error.

Did you mean n = p_name[x]?

Upvotes: 0

Moosa Saadat
Moosa Saadat

Reputation: 1177

p_name is a list. So you need to use square brackets:

n=p_name[x]

Upvotes: 1

Related Questions