Computer >> Computer tutorials >  >> Programming >> Python

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