Home > Blockchain >  How do you refer to the python script that you are programming inside the code without filenames? (r
How do you refer to the python script that you are programming inside the code without filenames? (r

Time:05-25

I do not know how else to describe the question in words, so let me show you what I mean. I have a file called "pugs.py". I am making it for other people so they might change the name for it, but I need the file to be able to refer to itself inside of the code. I could make sure it is always name, but I don't want that. So, how would I exactly refer to my "pugs.py" without saying "pugs.py", but instead something like "os.thisfile"?

CodePudding user response:

Python scripts and modules have a bit of information preloaded in their global namespace. __file__ should give the filename.

import os
print(os.path.basename(__file__))

CodePudding user response:

you can use __name__ variable to get the file name in python.

  • Related