Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NameError: name '__file__' is not defined , os.path.dirname(os.path.abspath(__file__))

I’m trying to use os.path.dirname(os.path.abspath(file)) for tracking my file txt but i don’t why its not working with me:

  import os

  print(os.getcwd()) # Get Working directory

  print(os.path.dirname(os.path.abspath(__file__)))
  file = open("Youness.txt") 

  NameError: name '__file__' is not defined.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There is a good chance that __ file __ is not defined because you execute your code in an interactive shell.

Write it in a file and execute it with python and the const __ file __ will be defined.

Hope it helps.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading