469,275 Members | 1,619 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

TypeError: 'tuple' object is not callable (while looping using for)

Hello World! (yes, I am a completely new to the programming scene, and this is my first post ever)

Working in Python, I want to create a separate .txt file for each of the items in a list of strings. Simplified setup:

list = ['item1', 'item2']
type(list)
type(list[0])
type(list[1])
for item in list:
g = open(item,'w')
pass

So, after verifying that the list is of type list and that its elements are strings, I still get the message that I am trying to call a tuple object.

After spending most of the last few days on this I am simply at a loss at what to do. Can you point me in the right direction?
Jan 1 '08 #1
6 23587
.. the output i get is the following:

>>> list =['item1','item2']
>>> type(list)
<type 'list'>
>>> type(list[0])
<type 'str'>
>>> type(list[1])
<type 'str'>
>>> for item in list:
... g = open(item,'w')
... pass
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'tuple' object is not callable
Jan 1 '08 #2
bvdet
2,851 Expert Mod 2GB
Please use code tags as noted in the reply guidelines.

Per Python documentation: The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets.

You should not use list as a variable name. This works:
Expand|Select|Wrap|Line Numbers
  1. lst = ['item1.txt', 'item2.txt']
  2. print type(lst)
  3. print type(lst[0])
  4. print type(lst[1])
  5. for item in lst:
  6.     g = open(item,'w')
  7.     g.write('Test')
  8.     g.close()
Jan 1 '08 #3
bvdet
2,851 Expert Mod 2GB
.. the output i get is the following:

>>> list =['item1','item2']
>>> type(list)
<type 'list'>
>>> type(list[0])
<type 'str'>
>>> type(list[1])
<type 'str'>
>>> for item in list:
... g = open(item,'w')
... pass
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'tuple' object is not callable
Did you do something like the following?
Expand|Select|Wrap|Line Numbers
  1. >>> open = (1,2,3)
  2. >>> for item in lst:
  3. ...     g = open(item,'w')
  4. ...     g.close()
  5. ...     
  6. Traceback (most recent call last):
  7.   File "<interactive input>", line 2, in ?
  8. TypeError: 'tuple' object is not callable
  9. >>> 
Jan 1 '08 #4
Did you do something like the following?
Expand|Select|Wrap|Line Numbers
  1. >>> open = (1,2,3)
  2. >>> for item in lst:
  3. ...     g = open(item,'w')
  4. ...     g.close()
  5. ...     
  6. Traceback (most recent call last):
  7.   File "<interactive input>", line 2, in ?
  8. TypeError: 'tuple' object is not callable
  9. >>> 
Thanks for the quick reply :)

Ok: The raw data are files that each contain several observations of each of a certain set of items. What I want is a separate file for each item that contains all the observations from all the raw data files.

I create a list of the items that I want separate files of, and using glob I create a list of the file names in the raw data folder. Using the code below I get the the 'tuple' error message first for line 9 and when I try to run it again I get the same error message for line 7.

(Being a 'newbie' i am currently writing the script in textwrangler and pasting it into the python interpretor in terminal on my mac to run the script. I have heard about #!/usr/local/bin/python but do not know how to go about implementing that)


Expand|Select|Wrap|Line Numbers
  1. itemlist = ['1','4']
  2. import glob
  3. datafiles = glob.glob('raw/*.txt') 
  4. for n in itemlist:
  5.      itemfile = 'trans/no.txt'
  6.      itemfile = itemfile.replace('no', n)
  7.      g = open ( itemfile, 'w' )
  8.      for x in datafiles:
  9.          f = open ( x, 'r' )
  10.          for line in f:
  11.              data = line.split(';')
  12.              if data[0] == n:    
  13.                  trans = ';'.join(data) + '\n'
  14.                  g.write(trans) 
  15.                  pass
Jan 1 '08 #5
... i now figured out how to run .py scripts from the command line in the terminal, and after a few rounds (yes, it kept giving me the tuple error...), it now seems to be working ok. thanks :)
Jan 1 '08 #6
bvdet
2,851 Expert Mod 2GB
... i now figured out how to run .py scripts from the command line in the terminal, and after a few rounds (yes, it kept giving me the tuple error...), it now seems to be working ok. thanks :)
I am glad you were able to solve the issue. The logical explanation of the error you were receiving is the name open was somehow assigned to an object. That would mask the built-in function open(). If this happens again, you can unmask the built-in function by using del.
Expand|Select|Wrap|Line Numbers
  1. >>> open
  2. <type 'file'>
  3. >>> open = 'a string'
  4. >>> open
  5. 'a string'
  6. >>> del open
  7. >>> open
  8. <type 'file'>
  9. >>> 
Jan 2 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Atul Kshirsagar | last post: by
147 posts views Thread by Michael B Allen | last post: by
5 posts views Thread by Randall Parker | last post: by
1 post views Thread by Gary Wessle | last post: by
10 posts views Thread by Charles Russell | last post: by
2 posts views Thread by AWasilenko | last post: by
33 posts views Thread by christophertidy | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.