Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Directory path
#4
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:

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!
Reply


Messages In This Thread
Directory path - by gahhon - Mar-01-2019, 02:49 PM
RE: Directory path - by gahhon - Mar-03-2019, 03:14 PM
RE: Directory path - by snippsat - Mar-03-2019, 03:49 PM
RE: Directory path - by DeaD_EyE - Mar-03-2019, 11:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  working directory if using windows path-variable chitarup 2 740 Nov-28-2023, 11:36 PM
Last Post: chitarup
  create a default path with idle to a specific directory greybill 0 877 Apr-23-2023, 04:32 AM
Last Post: greybill
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,215 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  In put path and directory Led_Zeppelin 1 2,476 Apr-23-2021, 03:25 AM
Last Post: Larz60+
  How do I set the path to my module one directory below my python 3.7.3 program? deac33 1 2,027 Jul-30-2019, 04:13 AM
Last Post: Larz60+
  Directory path EOF Graham 8 4,699 Nov-28-2018, 10:13 PM
Last Post: Graham
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,775 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020