TypeError: 'file' object is not callable

This is the place for queries that don't fit in any of the other categories.

TypeError: 'file' object is not callable

Postby hellb0y77 » Thu Jun 12, 2014 8:37 am

I'm new in python and this is my first script. This script reads an AWS SQS queue and returns the result, I would write the result in a file, but I get the error in question, this is the code:

Code: Select all
#!/usr/bin/python

import boto.sqs
import datetime

now = datetime.datetime.now()
now = str(now)

outfile = open("/root/SQS/messages.txt","w")

conn = boto.sqs.connect_to_region("eu-west-1",aws_access_key_id='.....',aws_secret_access_key='.......')

my_queue = conn.get_queue('name_queue')
rs = my_queue.get_messages()
if len(rs) == 0:
        print >> outfile (now,"- No new messages\n")

else:
        m = rs[0]
        print >> outfile (now,"-",m.get_body_encoded()+"\n")

if len(rs) > 0:
        my_queue.delete_message(m)

outfile.close()


Error:
Code: Select all
File "./read-message.py", line 16, in <module>
    print >> outfile (now,"- Non ci sono messaggi\n")
TypeError: 'file' object is not callable

Thanks
Last edited by Mekire on Thu Jun 12, 2014 8:57 am, edited 1 time in total.
Reason: First post lock.
hellb0y77
 
Posts: 6
Joined: Thu Jun 12, 2014 8:30 am

Re: TypeError: 'file' object is not callable

Postby stranac » Thu Jun 12, 2014 9:01 am

This code is trying to call outfile with two arguments, and then write to the resulting file.
Code: Select all
print >> outfile (now,"- No new messages\n")

You probably wanted something like this (not sure, since i never use this syntax):
Code: Select all
print >> outfile now, "- No new messages\n"


But it's always better to just use the .write() method:
Code: Select all
outfile.write('{}- No new messages\n'.format(now))
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: TypeError: 'file' object is not callable

Postby Mekire » Thu Jun 12, 2014 9:08 am

Wah... just typed up an almost identical reply...

Stranac wrote:not sure, since i never use this syntax


Yeah, I had to confirm this was even legal in Python 2. Looks so unnatural.
I thought someone had mixed up C++ syntax with python at first.

-Mek
User avatar
Mekire
 
Posts: 1008
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: TypeError: 'file' object is not callable

Postby hellb0y77 » Thu Jun 12, 2014 3:28 pm

But it's always better to just use the .write() method:
Code: Select all
outfile.write('{}- No new messages\n'.format(now))


Perfect, it's work!

Thank's!!
hellb0y77
 
Posts: 6
Joined: Thu Jun 12, 2014 8:30 am

Re: TypeError: 'file' object is not callable

Postby hellb0y77 » Thu Jun 12, 2014 4:00 pm

Ah...another problem, the script write the resul in two cases, if no message write "no mesage in file", else write the message. If no message i not have error and write correctly the file, if a message is present i have this error:

File "./read-message.py", line 29, in <module>
outfile.write('{1} -',m.get_body_encoded(),'\n'.format(now))
TypeError: function takes exactly 1 argument (3 given)

The script modified is:

Code: Select all
#!/usr/bin/python

import boto.sqs
import datetime

now = datetime.datetime.now()
now = str(now)

outfile = open("/root/SQS/messages.txt","a")

conn = boto.sqs.connect_to_region("eu-west-1",aws_access_key_id='......',aws_secret_access_key='.....')

my_queue = conn.get_queue('name_queue')

rs = my_queue.get_messages()
if len(rs) == 0:
        outfile.write('{0} - No messages\n'.format(now))
else:
        m = rs[0]
        outfile.write('{0} -',m.get_body_encoded(),'\n'.format(now))

if len(rs) > 0:
       my_queue.delete_message(m)

outfile.close()
hellb0y77
 
Posts: 6
Joined: Thu Jun 12, 2014 8:30 am

Re: TypeError: 'file' object is not callable

Postby snippsat » Thu Jun 12, 2014 4:40 pm

Code: Select all
>>> outfile.write('', '', '')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: function takes exactly 1 argument (3 given)

You must give one argument to write.
Try this.
Code: Select all
outfile.write('{0} - {1}\n'.format(now, m.get_body_encoded()))
User avatar
snippsat
 
Posts: 262
Joined: Thu Feb 21, 2013 12:04 am

Re: TypeError: 'file' object is not callable

Postby hellb0y77 » Thu Jun 12, 2014 5:13 pm

It's OK, thank's!!
hellb0y77
 
Posts: 6
Joined: Thu Jun 12, 2014 8:30 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], snippsat and 3 guests