473,499 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding name of running script

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
3 1654
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2358
by: Fuzzyman | last post by:
What's the best, cross platform, way of finding out the directory a script is run from ? I've googled a bit, but can't get a clear answer. On sys.argv the docs say : argv is the script name...
1
1153
by: Ken Kast | last post by:
I have a frameset page with the following code snippets: .. .. .. <script type="text/javascript"> <!-- var bannerFrame; function load () { var bannerDoc = bannerFrame.document;
3
24660
by: NeverLift | last post by:
But, if it's not open, I don't want to open it . . . using window.open will open it if it doesn't exist, even if the url in that open is null (the window is then empty -- but it's open). The...
22
2639
by: Tony Houghton | last post by:
I'm using pygame to write a game called Bombz which needs to save some data in a directory associated with it. In Unix/Linux I'd probably use "~/.bombz", in Windows something like "C:\Documents...
3
1842
by: Tim | last post by:
Hello Everyone I'd like to create a script/program that can locate all servers running DB2, finding out which instances are running/created and what databases are running under them but have...
9
2441
by: stevewy | last post by:
I am trying to write a function that will test all the checkboxes in a particular group of a form (it's a questionnaire), see whether more than three of them are ticked, and display a message if...
17
1830
by: NetworkElf | last post by:
Hi all, I'm writing a service that needs to discover the full directory path for a given locally based share at startup. IOW, I need to have the service running on someserver to take ...
12
1856
by: Jeff | last post by:
As I'm learning PHP, I'm making a fair number of mistakes in syntax. In perl, you can turn on reading these errors from the browser by adding this: use CGI::Carp 'fatalsToBrowser'; Is there...
1
1927
by: Lagon666 | last post by:
Get running object name Hi Is it possible to get running object name using a command that defined in same object? <script> var test = function() {
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.