Python directory: script

I was looking for a solution, but could not find what I needed.

Script path: / dir / to / script / script.py or C: \ dir \ script.py

Excluded result:

$ ./script.py output: /dir/to/script $ cd .. && ./script/script.py output: /dir/to/script 

Is there any function in the os module or something like that?


I mixed solutions and write:

 print os.path.abspath(os.path.dirname(__file__)) 

But it is ugly. Is there a better way?

+7
source share
1 answer

os.path.realpath will give you the result:

 os.path.dirname(os.path.realpath(__file__)) 
+15
source

All Articles