Re: How to store data files in packages
- To: mathgroup at smc.vnet.net
- Subject: [mg103691] Re: How to store data files in packages
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Fri, 2 Oct 2009 08:25:52 -0400 (EDT)
- References: <ha213g$n9i$1@smc.vnet.net>
Hi, > my packages uses some data files. Currently, I store them in a > subdirectory "resources" below the package directory and access the > data via > > ToFileName[{$UserBaseDirectory, "Applications", myPackage, > "resources"}, myDataFileName]; > > I wonder if this is a portable way. I am afraid it is not, since the > package may be installed under $BaseDirectory as well. And then there > is this directory at the same level as "Applications" named > "ApplicationData" > What is the recommended way to store my data files? I don't know, and I think it depends on what the format of your data files is. If they contain data but are normal package files ending in .m you can just use e.g. Get["myPackage`resources`file1`"]. For arbitrary file formats that can not be read with Get you probably need some more effort, but something like the following should do the trick, no matter where your package is installed (as long as the package itself can be loaded in the first place): datadir=First[FileNames[FileNameJoin[{myPackage, "resources"}], $Path]] Then you can import your data with e.g.: Import[FileNameJoin[{datadir,filename}]] While this should be pretty robust to find a "myPackage/resources" directory (if there is _one_), of course it still can possibly find a wrong "myPackage/resources" directory if there is another one elsewhere in $Path (very unlikley but possible). So depending on how your package is organized you might want to search for something that is more specific to your package and deduce the correct directory from that (e.g. myPackage/Kernel/init.m). Or you think of it as a special feature that allows the user to collect his own datafiles in e.g. $UserBaseDirectory while leaving the package and the original datafiles in e.g. $BaseDirectory... (it would be just another call to FileNames to search both or all myPackage/resources directories you can find in $Path...) Note that I have switched from ToFileName to FileNameJoin, since: 'As of Mathematica 7, ToFileName has been superseded by FileNameJoin.' Anyway, ToFileName still works alright and I can hardly imagine they will remove it very soon... hth, albert