
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to print its script name as output using Python
Our task is to develop a program in Python to print its script name where it is being executed. The coding part is simple. We use
int main(int argc, char** argv)
This function passes multiple parameters. First parameter is the number of arguments passed to the program, second parameter is the array which contains the name of all the arguments passed to the program.
Example code
import sys def main(): my_program = sys.argv[0] my_index = my_program.rfind("\") + 1 # slicing the filename My_program = my_program[my_index:] print("Program Name: % s" % my_program) # main block if __name__ == "__main__": main()
Output
Program Name: C:/Users/TP/Desktop/PYTHON FOLDER/python241-280/python277.py
- Related Articles
- Program to print its script name as output in Python
- Python program to print rangoli pattern using alphabets
- C program to print name inside heart pattern using for loop.
- Python program to print the initials of a name with last name in full?
- Get output of MongoDB shell script?
- Python program to print Possible Words using given characters
- How to generate JSON output using Python?
- Python program to print design door mat texture using characters
- Python program to print Rows where all its Elements’ frequency is greater than K
- Python Program to Print Hello world
- Python program to print number triangle
- Python program to print check board pattern of n*n using numpy
- Python program to print a checkboard pattern of n*n using numpy.
- How to print results of script in MongoDB?
- Python Program for Print Number series without using any loop

Advertisements