File existence checking

is there a way to check if a file exists, respecting the model-path? I found the function exists() in Filename but I guess it doesn’t respect model-path either.

Beside that, is there a LoaderOption for not throwing any error if a loading attempt fails - independent of the loaded item type? I was able to catch the IOError with a simple try-except-pass construct, but I still get notifier errors from gobj. Do I really have to turn the notifier off for the time I load my stuff?

Here an example from practice where such error quieting would be useful:

for ext in ("png", "jpg", "tga"):
        tex = loader.loadCubeMap("skyboxes/%s/#.%s" % (source, ext))

Pass okMissing = True to the loader call to allow it to return None if the file is missing.

You can also search the model path first, but that’s more work.


Couldn’t find that in any reference pages.
Where does this option come from?

Still, the gobj notifier errors persist. I guess there’s no way around disabling it temporarily here. Not sure what you mean by “where does it come from”.


Oh, well, if you really want to check for filename existence without incurring the notify error messages, you could just do it this way:

f = Filename('skyboxes/%s/0.%s' % (source, ext))
if vfs.resolveFilename(f, getModelPath()):
    print "Full path found at %s" % (f)
    tex = loader.loadCubeMap(f)
    print "File not found."


Well, there are two Loader classes in the reference and the page of the one from showbase seems to be very incomplete. That’s why I wasn’t able to find “okMissing” or the like through doxygen’s search function.

The sample code looks nice. Thanks!

ps: talking in pseudo-realtime here is kinda funny :slight_smile:

Just in case someone else is looking into this.
David’s sample doesn’t really work, but investigating it i found this little neat function:


It returns the full path (well, actually a Filename object, but who cares) if a file was found, so you can wrap a bool() around it.
A similar function, findAllFiles() in ConfigVariableSearchPath as returned by getModelPath() also allows searching multiple files on the model-path (I only can’t figure out how to use it… :blush:)


That works too, though it searches the actual disk only, rather than the vfs. This means it won’t work in a p3d file or in some other virtual environment.

I see the mistake in my own sample, my apologies. The correction should be:

if vfs.resolveFilename(f, getModelPath().getValue()):
    print "Full path found at %s" % (f)
    tex = loader.loadCubeMap(f)
    print "File not found."


Good point

Thank you! I’ll go with that then.

String path = “c:\temp\MyTest.txt”;
if (new File(path).exists())
System.out.println(“file exists”);

As per the my knowledge you can do in this way also .

Um, is that Java?

It’s certainly not python.

import os

if os.path.isfile('c:\\windows\\desktop\\somefile.txt'):

    #Do stuff

That works too.

Uhm, os.path.isfile doesn’t work in an OS-independent way, does not look in the VFS and does not respect the model-path.