for example, a.boo
method calls b.foo
method. In b.foo
method, how can I get a's file name (I don't want to pass __file__
to b.foo
method)...
To get the filename without extension in python, we will import the os module, and then we can use the method os. path. splitext() for getting the name. After writing the above code (Python get filename without extension), Ones you will print “f_name” then the output will appear as a “ file ”.
The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.
You can use the inspect
module to achieve this:
frame = inspect.stack()[1] module = inspect.getmodule(frame[0]) filename = module.__file__
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With