Артур Гудиев Артур Гудиев - 3 years ago 217
Python Question

Current filename from the library function in Python

Function f in lib.py returns os.path.basename.

When I call f() from 1.py it returns me 'lib.py'.
How can I modify f() so that it will return me '1.py'?

Answer Source

I guess, you need sys.argv[0] — a CLI argument #0 is the script being executed.

This also does not depend if it is called as python 1.py or ./1.py. For both cases, sys.executable can be used by find the python binary file. The script will always be argument #0.

It will be an empty string when called from an interactive mode, though. Because there is no script being executed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download