Path to current file depends on how I execute the program

This is my Python program:

#!/usr/bin/env python

import os

BASE_PATH = os.path.dirname(__file__)
print BASE_PATH

If I run this using python myfile.py it prints an empty string. If I run it using myfile.py , it prints the correct path. Why is this? I'm using Windows Vista and Python 2.6.2.


It's just a harmless windows quirk; you can compensate by using os.path.abspath(__file__) , see the docs


os.path.normpath(os.path.join(os.getcwd(),os.path.dirname(__file__)))

在许多情况下,最好使用:

os.path.dirname(sys.argv[0])
链接地址: http://www.djcxy.com/p/54696.html

上一篇: 如何知道在Python中运行脚本的路径?

下一篇: 当前文件的路径取决于我如何执行程序