0

I have a python project where I have numerous classes in a structure that can be simplified to

Root of project
 |------main.py
 |
 |------folder1
 |         |-------class1.py
 |         |
 |         |-------SubFolder1
 |                     |
 |                     |--------subClass1.py
 |                     |--------config1.json
 |                     |--------config2.json
 ...                   ...

subClass1 has to read and even modify numerous json files.

I am using paths relative to the folder where the I/O is done, i.e. subFolder1 which contains subClass1. However, for some reason, the path has to be changed depending on where I execute the initial code.

If I run "__main__" == __name__: in subClass1.py to execute the methods I want, there is no issue, but when testing from class1.py, I have to go back to the paths and modify them.

Is there a way around this without writing absolute paths, as I may rearrange the directory structure, and it would be nice if I wouldn't have to mess around with changing the paths.

user5423
  • 135
  • 13
  • The working directories would be different, right? If you do an OS "pwd" in the code, you can print it out and see whether that's the case. – Carlos Jul 29 '20 at 11:51
  • @user5423 I would recommend checking out [(os.path docs)](https://docs.python.org/3.7/library/os.path.html) and the `os.path.abspath()` function in particular. Dependent on how your code is structured internally it may also be worth looking into [(sys docs)](https://docs.python.org/3.7/library/sys.html). – JPI93 Jul 29 '20 at 12:14

0 Answers0