开发者

Where does python get the value for path when running Django/WSGI

In brief:

views.py:

def display_path(request):
    import os
    return HttpResponse("The path is %s" % os.path.abspath("."))

Results in

The path is /var/www

开发者_开发百科

Is it possible to change that value, or is it set by httpd/WSGI?


You should never rely on the current working directory being a specific location in web applications because what it will be will be different for different hosting mechanisms. So, don't even try to change the current working directory as it will only lead to grief eventually by then relying on that.

Instead you should organise your code to use an absolute path. This would need to be either hard coded, be added as a suffix to some prefix from configuration, or calculated on the fly relative to the location of the code file being executed. In the latter you would do:

import os
here = os.path.dirname(__file__)
path = os.path.join(here, 'relative/path/file.txt')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜