0

I would like to print the path and file name of my python code.

For example, " my_directory/my_subdirectory/my_python_code_name "

I tried different things, like import sys, os followed by any of those below:

sys.argv[0] 
print('sys.argv[0] =', sys.argv[0])  
print('full path =', os.path.abspath(pathname)) 
os.path.realpath(__file__)  

None of those works. I am using spyder.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Rick
  • 79
  • 9
  • How exactly these approaches do not work for you? What you get and what you expect instead? – PM 77-1 Dec 09 '20 at 23:58
  • Does this answer your question? [How to get full path to python program including filename within the program?](https://stackoverflow.com/questions/37754733/how-to-get-full-path-to-python-program-including-filename-within-the-program) – PM 77-1 Dec 09 '20 at 23:59

1 Answers1

1

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.

ClaudiaR
  • 3,108
  • 2
  • 13
  • 27
  • To @Claudia: a) print ('Full path without file name:', os.getcwd()) ==> This work fine. b) print('Full path:', __file__) ==> produces the following error: NameError: name '__file__' is not defined. c) The last command gives same error. Is there a solution? – Rick Jan 27 '21 at 02:02
  • To @Rick From your answer I think that you typed `file` instead of `__file__`. There are two low dashes "_" before and after the "file" word. – ClaudiaR Jan 27 '21 at 09:25
  • To @Claudia. Sorry, for some reason the low dashes dont show up in my copy/paste it here. I do have them, and I get the error message saying it is not defined. – Rick Jan 27 '21 at 18:43
  • @Rick, I think that maybe you're trying to execute the script from an interactive prompt. In fact `__file__` is defined only for Python scripts and modules. I have updated my answer, let me know if it solved the issue. – ClaudiaR Jan 27 '21 at 21:48
  • To @Claudia: I am using Spyder. Your first suggestion produces UsageError: %%javascript is a cell magic, but the cell body is empty. Your second suggestion produces NameError: name 'sys' is not defined Any ideas? Thanks. – Rick Jan 27 '21 at 22:38
  • @Rick try importing `sys` with `import sys` before the last command – ClaudiaR Jan 28 '21 at 07:57
  • This produces just C:\Users. See the commands below: import os import sys complete_path = os.path.dirname(os.path.abspath(sys.argv[0])) print(complete_path) C:\Users – Rick Jan 28 '21 at 19:53
  • @Rick I've updated my answer. The fact is that you are trying to display the name of a file that doesn't really exists, since that you are executing from an interactive console. What do you need the file name for? – ClaudiaR Jan 28 '21 at 21:13
  • To @Claudia: To display it in the generated reports/plots so I know in future what code it was generated with. In SAS I was able to do it easilly. Extremely useful. – Rick Jan 29 '21 at 15:50
  • @Rick maybe I'm missing something. Are you running your code from a `.py` script (which is a file and has a name) or are you executing cell by cell from an iPython console? In this case there is no file, just a console. If you want to keep record of your code you should use a script or save the iPython session to a file (which is not done by default). – ClaudiaR Jan 29 '21 at 16:02
  • I have a file.py with the code. I am running it in Spyder. It is not a cell by cell run. I run the code program as a whole. Notice that os.getcwd()) works fine for me. Gives dir, but not filename. I need to include file name, that is: my_file.py – Rick Jan 29 '21 at 16:47