?
Solved

Get filename of currently running python script

Posted on 2007-10-02
13
Medium Priority
?
6,408 Views
Last Modified: 2012-06-21
How do I get the filename of the currently running python script?
0
Comment
Question by:jameswalt
  • 3
  • 3
  • 3
  • +3
13 Comments
 
LVL 6

Expert Comment

by:msklizmantas
ID: 19999024
hi,

here you go:

import sys
import os

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

regards,

m

0
 

Author Comment

by:jameswalt
ID: 19999176
It's a little more complicated than that - this script is being run by Websphere wsadmin tool, running this command gives the following output:

WASX7017E: Exception received while running file "C:\testFiles\deployTbsmBackend
.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<string>", line 19, in ?
IndexError: index out of range: 0
0
 
LVL 6

Expert Comment

by:msklizmantas
ID: 19999259
try this:

import sys
print sys.argv

and paste output here please.

m
0
Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

 

Author Comment

by:jameswalt
ID: 19999374
[]
0
 

Author Comment

by:jameswalt
ID: 19999375
(just an empty array)
0
 
LVL 6

Expert Comment

by:msklizmantas
ID: 19999415
probably wsadmin just uses python (jython) script as library, so that's why you don't get filename. don't know what to offer to you in this moment, sorry.

m
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 19999449
Does:

print __name__

or:

print __path__

give anything useful?
0
 
LVL 29

Expert Comment

by:pepr
ID: 20001700
A would try the last RichieHindle's suggestion. For the sys.argv[0], I have found the following explanation http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.ws.ast.jythontools.doc/topics/rdebugscript.html

Cite: "When using the Jython debugger, the sys.argv[0] program argument specifies the filename of the script being debugged, and the sys.argv[1] is the first program parameter. However, if you are not debugging the Jython script the specification of the program arguments is different, the sys.argv[0] is the first program parameter ..."

Let's say, Jython is quite old ;)
0
 
LVL 15

Expert Comment

by:mish33
ID: 20004143
sys.executable
0
 
LVL 29

Expert Comment

by:pepr
ID: 20004176
mish33: No. The sys.executable returns name of the interpreter, not the name of the script.
0
 

Expert Comment

by:reshmir
ID: 20033466
Try this..

import os
import sys
AppName =  os.path.abspath(sys.argv[0])
print AppName

For only App. name:
print os.path.split(AppName)[1]  



0
 
LVL 29

Accepted Solution

by:
pepr earned 200 total points
ID: 20036264
reshmir: Have a look at http:#19999176. The question is related to Jython where tha sys.argv sometimes does not contain the name of the script.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 20123847
jameswalt: Could you post your solution?  I'm interested to know how you solved it.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question