1

I am having some trouble using a class I have created which includes a function that calls a folder of geojson files. The folder lives in the same directory as the .py file with the class and it is very important that I do not provide the full path for this folder.

If I do provide the full path, the function works perfectly. However, this is a shared folder with other users and for this reason, we need to keep the folder path like './geo_first_division'.

In more detail, I provide a scheme of the folders below and the folder named as "classes_folder" is the shared one. The file "class_geo.py" has a function within, say fun1(), which needs the content of the folder "geo_first_division".

The "app.py" is within the "app_folder" and although it works perfectly for all the functions within the class, it will break for the fun1() giving an error [Errno 2] No such file or directory: './geojson_first_division'.

There must be something that I do not see clearly here and I would appreciate some help.

enter image description here

Newbielp
  • 431
  • 3
  • 16
  • ANSWERING QUESTION -wrongly connected as dupl: I included in my class before any def the following: dir_path = os.path.dirname(os.path.realpath(__file__)), which actually gives the possibility to return the directory path of the current file. Within the def "fun1()", I used the following as folder_path: folder_path = f'''{self.dir_path}/geo_first_division'''. This way, the class can live in a different directory from the directory that a .py file lives with the latter calling the class. It also gives the possibility to other users to use it without making any changes in the code. – Newbielp Mar 16 '23 at 09:44
  • The second answer in the linked solution provides an approach similar to yours using `pathlib`. The question is correctly marked as a duplicate. – NotAName Apr 13 '23 at 04:47

0 Answers0