Python - Get The Full File Path A Function Was Called From?
Given a module mymodule.py; and in it def foo(): X = # file path where foo was called from print(X) How would I do what's described in the comment? Ie, if in test.py I di
Solution 1:
You could use sys.argv[0]
to get the main file 's name, then you could use os.path.realpath()
to get the full path of it:
import os
import sys
def foo():
X = os.path.realpath(sys.argv[0])
print(X)
Demo:
kevin@Arch ~> python test.py
/home/kevin/test.py
Post a Comment for "Python - Get The Full File Path A Function Was Called From?"