I am working on a project and one part of it is to have the program tell what it's own name is. I am later going to compile the PY file into an EXE, which I will rename. Is there any way for the program to tell what it's called?
Asked
Active
Viewed 2,294 times
1
-
1Sounds like `sys.argv[0]`? – Martin Valgur Dec 17 '15 at 02:41
2 Answers
2
sys.argv[0]
is the path it was invoked with. For output, you usually want to wrap that in os.path.basename
to avoid including the full path, sticking to just the name of the executable itself, e.g.:
#!/usr/bin/env python
import os, sys
print(os.path.basename(sys.argv[0]))

ShadowRanger
- 143,180
- 12
- 188
- 271
-
Ah, thank you! Turned out to be a lot easier than I thought it would. – Vladimir Shevyakov Dec 17 '15 at 02:49
1
The Python sys module provides access to any command-line arguments via the sys.argv. This serves two purposes −
sys.argv is the list of command-line arguments.
len(sys.argv) is the number of command-line arguments.
Here sys.argv[0] is the program ie. script name.
http://www.tutorialspoint.com/python/python_command_line_arguments.htm

downeyt
- 1,206
- 2
- 12
- 23