How to use python file module within package ?

Hi there.

A simple question this time :
How can I use the python built-in ‘File’ module when datas are packaged ?

It seems that files are mounted in a virtual way with th e virtualFileSystem (vfs) interface. So the given root path doesn’t match for file.open() that didn’t know the vfs.

I know that I can use vfs.getFile() from panda3d but I use some specifics of python file module… so is there a way somehow to get a ‘python’s File’ from a panda’s vfs ?

Thanks

Panda3D provides a replacement for the file module that does respect the virtual file system, it is automatically put in place in the plugin environment. For the record, though, these classes are in the direct.stdpy.file module:

from direct.stdpy.file import *

with open("/mf/somefile", "r") as b:
  print b.read()

They should provide the same functionality as the Python builtin ones, so there’s no extra effort required on your end.

Nice ! Thanks for the tip.

Unfortunatly it doesn’t work on my side :

from direct.stdpy.file import *

vfs.exists(filename) # -> 1
f = open(filename, "r+b")

return :

f = open(filename, "r+b")
  File "C:\buildslave\release_rtdist_win32\build\panda3d\built_cmu\direct\stdpy\file.py", line 127,
in __init__
IOError: No such file: /tmp/heightmaps/ASTGTM2_N45E005_dem.tif

edit : I mount files from http, may this have an influence ?

mmmh somethings strange here:

print vfs.exists(filename) # print 1
print (vfs.getFile(filename)).getFilename().exists() # print 0

What happens if you open the file in “rb” mode instead of “r+b” mode? The error message could also indicate the file exists but is not writable, which is true of an http-mounted file, and “r+” requires write permission.

As to your second case, you are stepping outside the vfs with the getFilename().exists() call, so it is not surprising it fails.

David

That’s right, ‘rb’ mode works. I was thinking that write mode were possible into a vfs (without saving it on the disk for example).
Also it’s a little confusing to know when panda use the vfs or not. However, thanks for your answer.

In 1.8.0, write mode is possible through the vfs, but only for files that are actually writable–files that are real files on disk, or in the virtual “Ramdisk” space.

If you really mounted a file via HTTP, write mode isn’t possible for that file, because HTTP doesn’t support that. (At least, not without the WebDAV extension, which Panda doesn’t attempt to support anyway.) And even if it were possible, would you really want to be writing directly to the server copy of your file?

David

Not for my HTTP files but for the others yes it should be usefull. That’s a good news that writing through vfs will be supported in 1.8.0.

Thanks again for your help

vash