2

the task is to read values from a ini file
Im working with Pycharm which for some reasons can not find a file from a relative path
I use pathlib.Path resolve to set the absolut path
because os.path.abspath("config_file/config.ini") doesnt work with my windows setup

filepath = pathlib.Path('../../myproject/config_file/config.ini').resolve()
works on my current machine but when I run the code on another device the ini file can not be found again.
on this answer Use resolve() are some errors mentioned that could acour while using this function. Are theese already resolved or Im just unaware of the functionality of pathlib resolve ?

1 Answers1

1

According to my experiment, comparing to os.path.abspath, resolve() is not that strict forward to use

Let's say you have following output for running below script, which means you are running on directory /home/aaa

os.path.abspath("config_file/config.ini")
# /home/aaa/config_file/config.ini

abspath returns absolute path to you without checking whether config_file/config.ini exists or not.

However, pathlib.Path.resolve() is not the same. If you want an absolute path from resolve(), such file /home/aaa/config_file/config.ini must be exist. Otherwise, resolve() returns relative path to you.

To pin back your question, I guess your program cannot locate such config.ini in your window deployment. Try to check out the result of os.getcwd() You will know why it fails.

EDITED: Python in window behaves as I described. But, in linux platform, it is able to return abspath anyway.

Mond Wan
  • 1,862
  • 2
  • 17
  • 21