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?

1
  • 1
    Sounds like sys.argv[0]? Commented Dec 17, 2015 at 2:41

2 Answers 2

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]))
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, thank you! Turned out to be a lot easier than I thought it would.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.