0

I have two functions which print into an excel file. THe only input is the file name. Here is the code:

#excelpy
import excelpy

#Tinker
from Tkinter import *
from tkSimpleDialog import *
from tkFileDialog import *

Function Mode1

def Mode1(full_name):
    print full_name
    print type(full_name) 
    testwbook = excelpy.workbook(full_name) 
    testwbook.show() 
    testwbook.set_cell((1,1),'TEST1', fontColor='red') 
    testwbook.set_range(2,1,['Number','Name']) 
    m1 = testwbook.save(full_name)
    testwbook.close()
    return m1

Function Mode2

def Mode2(full_name):
    print full_name
    print type(full_name) 
    testwbook = excelpy.workbook(full_name) 
    testwbook.show() 
    testwbook.set_cell((1,1),'TEST2', fontColor='red') 
    testwbook.set_range(2,1,['Number','Name']) 
    m2 = testwbook.save(full_name)
    testwbook.close()
    return m2

Main

root = Tk()
d = str(asksaveasfilename(parent=root,filetypes=[('Excel','*.xls')],title="Save report as..."))
d = d + '.xls'
d = d.replace('/','\\')
root.destroy()  

Mode1(d)
Mode2(d)

And once in a while I get the following error:

Traceback (most recent call last):
  File "T:\TEST\testpy.py", line 2035, in <module>
    Mode2(d)
  File ""T:\TEST\testpy.py"", line 1381, in Mode2
    print type(full_name) 
TypeError: 'str' object is not callable

Any idea why is this happening? How can I prevent it?

3
  • 3
    looks like 'type' is overridden somewhere as a str ... Commented Jul 29, 2011 at 15:48
  • Your tabbing is...strange. Please fix it, and give us a complete test case. Commented Jul 29, 2011 at 15:49
  • Never do from xxxx import *, you don't know what you're putting into your global namespace. Commented Jul 29, 2011 at 16:03

2 Answers 2

3

The only function call in the line you get the error is a call to the built-in function type(), so the only explanation for your error message is that you overwrote the built-in name type by a global name type pointing to a string object. Try adding

print type

before

print type(full_name)
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like somewhere you're setting a (global) variable named type to a string, thus overwriting the built-in type function.

Try searching your code for type = to see what turns up.

Understandably, Python would then throw that exception when you tried to call type (strings can't be "called").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.