0

When ran from a terminal, sys.argv[0] is the path of current script, but in python interactive that variable points to "/some/path/ipykernel_launcher.py" which is a temporary file.

How do I get the path of current script (which I am editing in vscode)? I need this information because whenever I create a file, I automatically log which script created it. For that, I overload the open() function to automatically log the creation. But when file is created from a python interactive session, I am missing such information.

agarrubio
  • 81
  • 1
  • 5

2 Answers2

1
import os

print(os.getcwd())

I think that's what you want. It prints the current working directory.

Ethitlan
  • 25
  • 6
  • Sorry, but that gives the execution path, which, by the way, I usually set with `os.chdir(some_dir)` . It is true that at start of the interactive session, the `os.getcwd()` is that of the script. ... Anyway, I am missing the filename. – agarrubio May 24 '21 at 06:39
0

The answer that works for me, obtained from How do I get the path and name of the file that is currently executing? is this:

os.path.realpath(__file__)

I tried all other suggestions, but didn't work.

agarrubio
  • 81
  • 1
  • 5