473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

name of this python program

leo
hi there

when i start a python script with
python myprog.py
how can i find out the name of my script ("myprog.py") inside the script
itself. looking for something like args[0]...

cheers, leo
Jul 18 '05 #1
5 6143
leo schrieb:
when i start a python script with
python myprog.py
how can i find out the name of my script ("myprog.py") inside the script
itself. looking for something like args[0]...


import sys
import os
print sys.argv[0]
print os.path.basename(sys.argv[0])

Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
-------------------------------------------------------------------
Jul 18 '05 #2
>

hi there

when i start a python script with
python myprog.py
how can i find out the name of my script ("myprog.py") inside the script
itself. looking for something like args[0]...

cheers, leo

import sys
print sys.argv[0]

However, it can be wrong if you run it that way ('python myprog.py'). It
works when you run it this way: 'myprog.py'.

Best,

G


Jul 18 '05 #3
Gandalf wrote:
import sys
print sys.argv[0]

However, it can be wrong if you run it that way ('python myprog.py'). It
works when you run it this way: 'myprog.py'.


I've never seen it "wrong", no matter how the script is run. Perhaps I
haven't payed close enough attention. At worst you'll get some extra path
information to the python script. One of the replies already mentioned using
os.path to get the name of the actual script from any path.
Jul 18 '05 #4
leo wrote:
hi there

when i start a python script with
python myprog.py
how can i find out the name of my script ("myprog.py") inside the script
itself. looking for something like args[0]...

cheers, leo

Depending on your python version, you might try for __file__.

--
-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #5
leo <le*@bella.local> wrote in message news:<m2************@bella.local>...
hi there

when i start a python script with
python myprog.py
how can i find out the name of my script ("myprog.py") inside the script
itself. looking for something like args[0]...

cheers, leo


try

print 'My modulname: %s' % __file__

Will give you the filename of your modul.

Reagards
Peter
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Darren Dale | last post by:
I have built two basic modules using distutils. One of them installs fine, but the other, I can't import modules from it. I've been pouring over the code and I cant find the problem. I know the...
21
by: André | last post by:
Short version of what I am looking for: Given a class "public_class" which is instantiated a few times e.g. a = public_class() b = public_class() c = public_class() I would like to find...
23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
9
by: Swaroop C H | last post by:
Hi, Is there a way to set the program name in Python, similar to $0 in Perl? >From `man perlvar`: $0 Contains the name of the program being executed. On some oper- ating systems...
9
by: sdb1031 | last post by:
I am trying to run an exe within a python script, but I'm having trouble with spaces in the directory name. The following example will display the usage statement of the program, so I know that...
10
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to...
70
by: jojoba | last post by:
Hello! Does anyone know how to find the name of a python data type. Conside a dictionary: Banana = {} Then, how do i ask python for a string representing the name of the above dictionary...
12
by: Stef Mientki | last post by:
In the example below, "pin" is an object with a number of properties. Now I want 1- an easy way to create objects that contains a number of these "pin" 2- an multiple way to access these "pin",...
2
cwoozy
by: cwoozy | last post by:
Hello - I am very new to Python, and I am trying to write a program that returns the Area and Volume of a sphere when the user inputs the radius. I thought I had a solid program while typing it...
3
by: oyster | last post by:
I don't remember its name very clear, it may be 'jingle' or not this program runs on windows and can compile a python program into exe file without gcc it has no webspace but is announced on the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.