1

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?

Vladimir Shevyakov
  • 2,511
  • 4
  • 19
  • 40

2 Answers2

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
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