RockOnGom
RockOnGom

Reputation: 3961

Get absolute path in Python

I'm having a problem with assigning static files containing resources.

My working directory structure is:

|- README.md
|- nlp
|   |-- morpheme
|   |-- |-- morpheme_builder.py
|   |-- fsa_setup.py
| - tests
|   |-- test_fsa.py
| - res
|   |-- suffixes.xml

The code for fsa_setup.py is:

class FSASetup():
    fsa = None
    def get_suffixes():
        list_suffix = list()
        file = os.path.realpath("../res/suffixes.xml")
        .....
if __name__ == "__main__":
    FSASetup.get_suffixes()

The code for morpheme_builder.py is:

class MorphemeBuilder:
    def get_all_words_from_fsa(self):
        ......
if __name__ == "__main__":
    FSASetup.get_suffixes()

When it is called in fsa_setup.py, the file path's value is '\res\suffixes.xml' and that is correct, but when the other case realized, the file path value is '\nlp\res\suffixes.xml'.

I know how it works like this. So how can I give the path of the resource to the file.

Upvotes: 0

Views: 97

Answers (1)

golobitch
golobitch

Reputation: 1334

The problem is that morpheme_builder.py is in the directory morphem. So when you say ../res/suffixes.xml it will go on directory back ... so it will go to nlp/res/suffixes.xml. What about if you use os.path.abspath("../res/suffixes.xml")?

Upvotes: 1

Related Questions