Python NameError: name ‘__file__’ is not definedのエラー
NameError
Pythonで、NameError: name ‘__file__’ is not defined のエラーになった時の備忘録。
カレントディレクトリに移動する際に以下のコーディング行うことがあるが、上記のエラーになってしまった。
os.chdir(os.path.dirname(os.path.abspath(__file__)))尚、問題なく動作する環境もあるので注意。
対応策
os.chdir(os.path.dirname(os.path.abspath('__file__')))または、
os.chdir(os.path.dirname(os.path.abspath("__file__")))以上。



最近のコメント