python __file__ is not defined solutions

python __file__ is not defined solutions

__file__ variable is a python module is imported when generated, it can not be used in __file__, but want to get the current path of the file should be how to do it:

method one:

import inspect, os.path

filename = inspect.getframeinfo(inspect.currentframe()).filename
path     = os.path.dirname(os.path.abspath(filename))

Method Two:

import inspect
import os

os.path.abspath(inspect.getsourcefile(lambda:0))

Reference Links: https://stackoverflow.com/questions/2632199/how-do-i-get-the-path-of-the-current-executed-file-in-python/18489147#18489147

Guess you like

Origin www.cnblogs.com/ibingshan/p/10937435.html