如何在Python中获得当前执行的文件的路径?
path = os.path.abspath(os.path.dirname(sys.argv[0]))
path = os.path.abspath(os.path.dirname(__file__))
py2exe
__file__
execute()
__file__
NameError: global name '__file__' is not defined
a.py: __file__= a.py a.py: os.getcwd()= C:\zzz b.py: sys.argv[0]= a.py b.py: __file__= a.py b.py: os.getcwd()= C:\zzz
#! /usr/bin/env pythonimport os, sysprint "a.py: sys.argv[0]=", sys.argv[0]print "a.py: __file__=", __file__print "a.py: os.getcwd()=", os.getcwd()printexecfile("subdir/b.py")
#! /usr/bin/env pythonimport os, sysprint "b.py: sys.argv[0]=", sys.argv[0]print "b.py: __file__=", __file__print "b.py: os.getcwd()=", os.getcwd()print
C:.| a.py \---subdir b.py
慕标琳琳
相关分类