how to organize a module that requires a data file

manuelg at gmail.com manuelg at gmail.com
Thu Nov 17 19:01:01 EST 2005


I have tried several ways, this is the way I like best (I develop in
Windows, but this technique should work in *NIX for your application)

:: \whereever\whereever\     (the directory your module is in,
                              obviously somewhere where PYTHONPATH can
                              see it)

:::: stevemodule.py          (your module)

:::: stevemodule_workfiles\  (a subdirectory in the same directory as
                              your module)

:::::: __init__.py           (an empty file in stevemodule_workfiles\,
                              only here to make stevemodule_workfiles\
                              look like a package)

:::::: stevelargetextfile.txt  (your large textfile in
                                stevemodule_workfiles\)

Now, to load the large textfile, I agree that it should be done with
module functions, so if it gets used several times in the same process,
it is only loaded once.  The Python module itself follows the
"singleton" pattern, so you get that behavior for free.

Here is the Python code for loading the file:

import os.path
import stevemodule_workfiles

workfiles_path =
    os.path.split(stevemodule_workfiles.__file__)[0]

stevelargetextfile_fullpath =
    os.path.join(workfiles_path, 'stevelargetextfile.txt')
    
stevelargetextfile_file = open(stevelargetextfile_fullpath)




More information about the Python-list mailing list