0

可能重复:
os.path.dirname(__file__) 返回空

这是我的一段代码:

def GetAppPath():
    application_path = None
    if getattr(sys, 'frozen', False):
        application_path = os.path.dirname(sys.executable)
    elif __file__:
        application_path = os.path.dirname(__file__)
    return application_path

在 Windows 上完美运行,但在 Debian 上返回空字符串。任何想法可能是什么问题?

我正在使用 Python 2.7.3、Debian 6.0.5

4

2 回答 2

1

在 Ubuntu 12.04/python 2.7 上为我工作:

import os.path

print os.path.dirname(__file__)

存储在 /tmp/foo.py 然后运行它:

$ python /tmp/foo.py 
/tmp
于 2012-10-12T15:21:46.327 回答
1

似乎 __file__ 没有定义或定义不正确,适用于 Debian 测试,Python 2.7.3rc2

    def GetAppPath(file):
        application_path = os.path.dirname(file)
        return application_path

GetAppPath('/etc/X11/xorg.conf') 返回 '/etc/X11'

于 2012-10-12T15:09:27.847 回答