-1

I am new to Python and recently I am working on Shell Scripting. In Shell Scripting if I want to pass filename to variable then I am passing like

myScriptName="`/bin/basename $0`"

Is it possible to same in Python??

shailavi shah
  • 171
  • 1
  • 4
  • 14

1 Answers1

1

If you don't care about path of the file: You can use a call to OS method __file__ to accomplish this. This will bring in extension also.

import os

fileName = os.path.basename(__file__)
print fileName

If you want full path with filename assigned to a variable:

import sys

file2= sys.argv[0]
print file2
Anil_M
  • 10,893
  • 6
  • 47
  • 74