I have written a Python package hwrt
which comes with a model.tar
file. Neither the file itself nor the contents should bother users (they can take a look at it, it's not a secret - but they don't need it).
I need to work with files in that archive. To do so, I extract the contents and work with them:
import tarfile
tar = tarfile.open('model.tar in python package')
tar.extractall()
tar.close()
[... work with those files ...]
This works. However, I see two problems with this approach:
- The
model.tar
get extracted every time. - The contained files get placed in the current working directory which might not be expected by the user.
To solve problem (2), it seems to me that creating a temporary folder in tempfile.gettempdir()
would be the cleanest solution. However, I am not sure if that is the place where this should be done.
Are there other archive formats where I can access the contents of the archive directly? Is there a directory where I can place files for my Python package (for all users) after it was installed?
If the file is only ~10MB, just read it into RAM. Your later thousands of accesses will thank you.
If you want to only unpack the first time, you have several options:
Unpack at install time into the source directory
Unpack once into a local
.cache
directoryIf you can bear unpacking once per run, use
tempfile.gettempdir
.