There are several different methods that you might want to know, depending on what you are trying to accomplish.
The inspect module has a getfile() function that can be used to determine the name of the currently executing function.
Example:
#!/usr/bin/env python3 import inspect print(inspect.getfile(inspect.currentframe()))
Result:
test.py
To find out which command line arguments were used to execute the script, you need to use sys.argv
Example:
#!/usr/bin/env python3 import sys print(sys.argv)
Result when calling ./test.py abc :
['./test.py', 'a', 'b', 'c']
Result when calling python3 test.py abc :
['test.py', 'a', 'b', 'c']
Hope this helps!
source share