English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The program uses Python to print its script name as output

Our task is to develop a program in Python that prints the script name at the location where the program is executed. The coding part is very simple. We use

int main(int argc, char** argv)

This function passes multiple parameters. The first parameter is the number of arguments passed to the program, and the second parameter is an array containing the names of all arguments passed to the program.

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

Program Name: C:/Users/TP/Desktop/PYTHON FOLDER/python241-280/python277.py