I'm not sure if this is late, but maybe this answer can help someone else. In general in order to get a file path you would do something like this:
import os
print('Full path:', __file__)
print('Full path without file name:', os.getcwd())
print('Name of the file:', os.path.basename(__file__))
However using __file__
may not work for you if you are using an interactive prompt as the prompt from Spyder or Jupyter Notebook.
In this latter case you could write in a cell the following:
%%javascript
IPython.notebook.kernel.execute('notebook_name = "' + IPython.notebook.notebook_name + '"')
With this you are defining the variable notebook_name
with the name of the .ipynb
file from which you are executing.
To better answer the question, in Spyder you can execute code from a temp python file which works exactly as a normal python script, and for which you can use the first fragment of code above.
From your answers however I understand that you are executing code from the Spyder IPython Console that doesn't really have a name of a file to display. You should consider using a script if you really want a name to display.