As anchor you can use different locations or you work with absolute paths, which makes your program less flexible.
If you use the current working directory, you have to be in the right path, if you execute the program. This is not very usable, if the program lives somewhere on your path. When your program is somewhere in /usr/bin or C:\Windows\system32\, the working directory differs from Path, where the executable file is.
For myself I use the executable itself as anchor and define, all directories next to the executable. Here an example:
If you plan to put your project into a zip file, together with resources, it's different.
But I guess you're not so far. First you should try pathlib, which helps a lot.
Edit: Instead of getting absolute paths of images, you can do it with the executable files. Another solution could be, to put the executable files in the system path, but this makes less fun.
If you use the current working directory, you have to be in the right path, if you execute the program. This is not very usable, if the program lives somewhere on your path. When your program is somewhere in /usr/bin or C:\Windows\system32\, the working directory differs from Path, where the executable file is.
For myself I use the executable itself as anchor and define, all directories next to the executable. Here an example:
import sys from pathlib import Path cwd = Path.cwd() exe = Path(sys.argv[0]).resolve().parent print('Current working directory:', cwd) print('Directory of executable:', exe) print('Directory for images next to executable', exe / 'images')If you have for example this structure:
Output:|- project_test/
|- bin/
|- your_program
|- docs/
|- Mastering_Natural_Language_Processing_with_Python.pdf
|- learning_numpy_array.pdf
|- images/
|- siemens_updates.jpg
|- 11106-256x256x32.png
|- x_vs_xor_vs_aes.png
The program:If you plan to put your project into a zip file, together with resources, it's different.
But I guess you're not so far. First you should try pathlib, which helps a lot.
Edit: Instead of getting absolute paths of images, you can do it with the executable files. Another solution could be, to put the executable files in the system path, but this makes less fun.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!