Luffy
Luffy

Reputation: 677

Python os.path.abspath unexpected result

When I start program like this

python Script.py

abspath return sth like that

os.path.abspath("../../house/kitchen") == "/ex1/ex2/house/kitchen"

But when i start like this i got

python ex3/Script.py 
os.path.abspath("../../house/kitchen") == "/house/kitchen"

I think i need to set working place to place where is script but how to do that.

os.chdir(os.path.dirname(os.path.realpath(sys.argv[0])))

This solved my problem

Upvotes: 0

Views: 2108

Answers (1)

stark
stark

Reputation: 13189

When given a relative path argument, abspath starts from your current directory, not the current directory of the script, so if your current directory changes the output changes.

Upvotes: 1

Related Questions