Weird Error loading file from path

This one is weird because I am using the same code somewhere else and it works:

def load_pkl_to_data(mcdb):
	print "mcdb ->", mcdb, type(mcdb)
	mcdbfile = open(getModelPath().findFile("assets/mcscripts/%s.pkl" % mcdb), "r")

load_pkl_to_data(mced)

Which gets me:

mcdb -> mcdbships <type 'str'>

mcdbfile = open(getModelPath().findFile("assets/mcscripts/%s.pkl" % str(mcdb)), "r")
TypeError: coercing to Unicode: need string or buffer, libpandaexpress.Filename found

Anybody got an idea whats going on here?

That’s strange all right, but perhaps it would be easier to see where the problem is if you didn’t cram so many operations into a single line. Rewrite it like this:

pathstr = "assets/mcscripts/%s.pkl" % mcdb
modelPath = getModelPath()
filename = modelPath.findFile(pathstr)
mcdbfile = open(filename, "r") 

And then when Python gives you the exception, you can see which part of all that it’s actually complaining about.

David

Looks like findFile is returning a panda “Filename”, which is not being accepted by the python open function. You might be able to put a str() around the Filename returned by findFile.

Something like this:

def load_pkl_to_data(mcdb):
   print "mcdb ->", mcdb, type(mcdb)
   file_to_find = "assets/mcscripts/%s.pkl" % mcdb
   filepath = getModelPath().findFile(file_to_find)
   mcdbfile = open(str(filepath), "r")

load_pkl_to_data(mced)

Actually, you would need to use filename.toOsSpecific() to make it into a string the open() would accept. Or, you could just put “from direct.stdpy.file import open” at the top of your file; the stdpy version of open() will accept a Filename directly (as well as a string).

David