Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (47.6k points)

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

For example, I have a script called foo.py and I would like to do something like this inside it:

print Scriptname

and get: foo.py.

1 Answer

0 votes
by (106k points)

For getting the name of the Python script that is currently running you can use __file__. And if you want to remove the directory part which might be present with the name of the script, you can use os.path.basename(__file__).

Another thing you can use sys.argv[] and it will print foo.py for python foo.py, dir/foo.py for python dir/foo.py, etc.

import sys 

print(sys.argv[0])

image

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 18, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 11, 2019 in Java by Ritik (3.5k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...