Mar-11-2021, 04:31 PM
I use a trick similar to this in small programs that use an image or sound file. Get the path to the local file and use that as the base for the files you want to open. I'm pretty sure there is no way to make it work the same way as an imported module. It's a bit of a hack anyway.
That doesn't mean it isn't useful at all. If you put your files in the same folder as this module it will work. For example, you have a folder containing all your image files and you want a simple way to get an image file. Write a version of this named imagefiles.py.
That doesn't mean it isn't useful at all. If you put your files in the same folder as this module it will work. For example, you have a folder containing all your image files and you want a simple way to get an image file. Write a version of this named imagefiles.py.
import os DIRECTORY = os.path.dirname(__file__) def get(relPath): return os.path.join(DIRECTORY, relPath)From your program that uses image files.
import imagefile abs_path = image_file.get('ship.png')