Get name of current script in Python

前端 未结 18 2400
北荒
北荒 2020-11-28 00:54

I\'m trying to get the name of the Python script that is currently running.

I have a script called foo.py and I\'d like to do something like this in ord

18条回答
  •  囚心锁ツ
    2020-11-28 01:12

    import sys
    print(sys.argv[0])
    

    This will print foo.py for python foo.py, dir/foo.py for python dir/foo.py, etc. It's the first argument to python. (Note that after py2exe it would be foo.exe.)

提交回复
热议问题