By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,585 Members | 1,809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,585 IT Pros & Developers. It's quick & easy.

Finding name of running script

P: n/a
Is it a more pythonic way of finding the name of the running script
than these?

from os import sep
from sys import argv

print argv[0].split(sep)[-1]
# or
print locals()['__file__'].split(sep)[-1]
# or
print globals()['__file__'].split(sep)[-1]

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> print locals()['__file__'].split(sep)[-1]
..split(sep)[-1] is pretty dense reading.
try:
print os.basename(locals()['__file__'])

runes wrote:
Is it a more pythonic way of finding the name of the running script
than these?

from os import sep
from sys import argv

print argv[0].split(sep)[-1]
# or
print locals()['__file__'].split(sep)[-1]
# or
print globals()['__file__'].split(sep)[-1]


M.E.Farmer

Jul 19 '05 #2

P: n/a
Thanks!

That's os.path.basename() I guess. It's better, but still complex.

I have a
_nof_ = argv[0].split(sep)[-1] in my script template and use it under
the usage() function to tell what the script does, like:

"cf.py counts files in directory or directory structure"

If I change the filename, I want it to be automatically reflected when
running the script. That's the motivation.

I have the feeling that I came a cross some very simple way to extract
the filename, but I can't find my mental note ;-)

Jul 19 '05 #3

P: n/a
runes wrote:
Thanks!

That's os.path.basename() I guess. It's better, but still complex.
Yea murphy's typo ;)
I have a
_nof_ = argv[0].split(sep)[-1] in my script template and use it under the usage() function to tell what the script does, like:
"cf.py counts files in directory or directory structure"

If I change the filename, I want it to be automatically reflected when running the script. That's the motivation.
Generally I do it the very same way just spell it different.
os.path.basename(sys.argv[0])
I have the feeling that I came a cross some very simple way to extract the filename, but I can't find my mental note ;-)

Maybe you found some black magic ;)
M.E.Farmer

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.