Skip to content Skip to sidebar Skip to footer

Django Os.path.dirname(__file__)

I am doing exercises from book: http://www.tangowithdjango.com/book17/chapters/templates_static.html and I have problem with this code: import os print __file__ print os.path.dir

Solution 1:

The __file__ value in the main script can be relative to the current working directory. Use os.path.abspath() to make it absolute first:

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

Solution 2:

This is the absolute path of the directory where the program resides which is what you want I believe.

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

This is the parent directory of the program

os.path.join(os.path.dirname(__file__), 'some_directory')

This is the abbreviated directory where the program resides

os.path.dirname(os.path.realpath(__file__))

Post a Comment for "Django Os.path.dirname(__file__)"