python NameError: global name '__file__' is not defined

前端 未结 12 837
误落风尘
误落风尘 2020-12-02 08:08

When I run this code in python 2.7, I get this error:

Traceback (most recent call last):
File \"C:\\Python26\\Lib\\site-packages\\pyutilib.subprocess-3.5.4\\         


        
12条回答
  •  粉色の甜心
    2020-12-02 08:12

    If all you are looking for is to get your current working directory os.getcwd() will give you the same thing as os.path.dirname(__file__) as long as you have not changed the working directory elsewhere in your code. os.getcwd() also works in interactive mode.

    So os.path.join(os.path.dirname(__file__)) becomes os.path.join(os.getcwd())

提交回复
热议问题