473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 24122
.. 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

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

Similar topics

1
by: Atul Kshirsagar | last post by:
Hello, I am using Python 2.3.2 with a C++ extention DLL in muti-threaded environment. 1. For each new thread I create a separate sub-interpreter. 2. Each thread executes multiple python...
147
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
5
by: Randall Parker | last post by:
Using Python 2.4.2 on Windows 2000 in SPE. Getting: TypeError: 'str' object is not callable on this line: TmpErrMsg1 = "State machine %s " (StateMachineName) In Winpdb 1.0.6 the...
1
by: Gary Wessle | last post by:
dear python users I am not sure why I am getting **************************************************************** Traceback (most recent call last): File "my.py", line 3, in ?...
10
by: Charles Russell | last post by:
Why does this work from the python prompt, but fail from a script? How does one make it work from a script? #! /usr/bin/python import glob # following line works from python prompt; why not in...
2
by: AWasilenko | last post by:
I'm trying to test a few different approaches to displaying pages via Cherrypy and I'm not having much luck. Here is my code so far: import sys, cherrypy, html class Root: @cherrypy.expose...
33
by: christophertidy | last post by:
Hi I am new to Python and have recieved this error message when trying to instantiate an object from a class from another file within the same directory and wondered what I have done wrong. I...
18
by: Charlie of Bolton | last post by:
Hi, everybody, Did work hard on this one, as I`m a newbies... I did write the entire below script... This script is suppose to ping: a primary IP (only one), (entered manually w raw-input) ...
1
by: Charles Fox | last post by:
Hi gys -- I am looking at Numpy but getting this error when I try to get array sizes. I'm using Ubuntu Edgy with standard repositories and scipy. Any ideas? Am I doing something wrong or is it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.