469,304 Members | 2,192 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

How do I refer to a data file relative to a package or installation directory?

I have a package that includes some data files. I am planning to use a
distutils setup script to install the package. How can I compute a path
name for such a data file that will always be relative to my package
root directory (i.e. both when installed, and in my development directory?)

Or to rephrase, say the data file is mypkg/data/foo.dat relative to the
package root "mypkg" (the directory that holds __init__.py). Is there an
enquiry that will return the absolute path prefix for mypkg?
-- Parzival
-- Reply-to is confuggled: parzp (@) shaw (.) ca

Jul 18 '05 #1
1 1401
Parzival wrote:
I have a package that includes some data files. I am planning to use a
distutils setup script to install the package. How can I compute a path
name for such a data file that will always be relative to my package
root directory (i.e. both when installed, and in my development directory?)

Or to rephrase, say the data file is mypkg/data/foo.dat relative to the
package root "mypkg" (the directory that holds __init__.py). Is there an
enquiry that will return the absolute path prefix for mypkg?


Presumably you meant that relative to the package root "mypkg",
the data file is actually at "data/foo.dat", or as some might
write it "./data/foo.dat"? If not, you've got an extra layer
there confusing things.

Basically, just use __file__ inside the __init__.py module to find
that file's location, then use os.path.split() to pull off the
path part and os.path.join() to add the "data/foo.dat" part.

If that's not enough, please post some kind of code snippet
to help explain things better.

-Peter
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Alexander Kienzle | last post: by
6 posts views Thread by Nadav | last post: by
6 posts views Thread by Darhl Thomason | last post: by
reply views Thread by Alan Isaac | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.