From the code below both returns me the same output. When should I use one over the other?
import os
from os.path import abspath, dirname
print abspath(dirname(__file__))
print os.getc开发者_如何学Gowd()
PS: I wanted to use this for dynamically changing the path for logs, static files and templates in my django project to run it in different environments.
Use the first when you want to know the location of the file being executed. Use the second when you want to know the process's current working directory.
os.getcwd
can be used at places where you want to move from your current directory to perform some actions on other directory, for this you save your current working directory or to know the location of the file being executed.
abspath
is used to get absolute path of file, gives you location information of file.
精彩评论