Search This Blog

Oracle Fusion Middleware Administration Course

Oracle Fusion Middleware Administration Course
OFMW Admin Course

Thursday, December 25, 2014

File IO in WLST: How to store output of WLST/Jython?

Hey WLST scripting enthos Guys!!

I was prepared some of industry most reffered WLST monitoring scripts... they are running well and good... Now the need is to store the result of this monitoring script into a FILE. How to make this? how to redirect this output... normally output is coming from print command in the jython/python. I was searching in google for this output storing to a file from WLST not really good outcomes... then I thought better I choose file style as in C programming allows Standard Output to a files with file pointers. This found a jython tutorial which is already available in online...

WLST File input output operations


Standard Out and Error

Standard output and standard error (commonly abbreviated stdout and stderr) are pipes that are built into every unix-like platform. The stdout (fileObject) is defined in the Python standard sys module, and it is a stream object. Calling its write() function which will print out whatever string you pass as argument to it, then return the length of the output. In fact, this is what the print function really does; it adds a carriage return to the end of the string that you’re printing, and calls sys.stdout.write function.
# This program is to illustrate sys fileojects
# Standard Output stdout
# Standard Error stderr
print "Normal print..."
for i in range(3):
        print "WLST is awesome!!!"

import sys
print "sys.stdout...."
for i in range(5):
        l=sys.stdout.write('WLST By Examples\n')

print "sys.stderr...."
for i in range(4):
        errline=sys.stderr.write('WLST in 12.1.3 Wonderful!! ')

Here it shows the difference of print, write function with/without carriage return example.
$ wlst

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Normal print...
WLST is awesome!!!
WLST is awesome!!!
WLST is awesome!!!
WLST By Examples
WLST By Examples
WLST By Examples
WLST By Examples
WLST By Examples
WLST in 12.1.3 Wonderful!! WLST in 12.1.3 Wonderful!! WLST in 12.1.3 Wonderful!!

In the simplest case, sys.stdout and sys.stderr send their output to the same place. In Python, standard output, standard error does not add carriage returns for you. If you want carriage returns. you’ll need to write manually include the carriage return characters. sys.stdout and sys.stderr are stream objects, but they are write-only. Here important fact is that when you attempting to call their read() method will always raise an IOError.

Redirecting output with file descriptor is fine worked good... 
now one more thing... need a timestamp for everytime when you run the monitoring script...
Solution again available in the same tutorial...
jython is going to support Java internally... Java's Date utility gives you the facility to have a time stamp redirect this print to the same file descriptor ... your job is done!!
fileObj = open('test.txt', 'w')
writeList=["I am an Indian", "I love my India", \
"I want to go to USA", "I want to earn dollars", \
"I want to die in India"]

for i in writeList:
 print >>fileObj, i


# Default file mode is read (r)
fileObj = open('test.txt')

# We can move fileObj position here it is 0

for lines in fileObj.readlines():
 print lines

Creates test.txt with lines in list and then will read lines from same file.
wls:/offline> execfile('')
I am an Indian

I love my India

I want to go to USA

I want to earn dollars

I want to die in India

The file attributes

Following table have the list of the file object attributes. Which we can directly access them public.
file.closedReturns true if file is closed, false otherwise.
file.modeReturns access mode with which file was opened.
file.nameReturns name of the file.
# This program illustrates the file attributes

print "mode:",f.mode
print "name:",

if f.closed:
        print, " is closed"
        print," is not closed"

Its execution gives the output as follows
wls:/offline> execfile('')
mode: r
name:  is not closed


with-as operation on Files

Following example shows how to use with

How do I test a simple Jython script at Jython prompt?

You can use the following lines to invoke Jython Shell on your WebLogic environment. This is possible because WLST is based on Jython Shell, obviously you can get a Jython prompt in the WebLogic environment.
Normally you can get with
$ java  org.python.util.jython

Otherwise use the following way to skip caching:
$ java -Dpython.cachedir.skip=true org.python.util.jython

Remember that, WebLogic 9.2 supports Jython 2.1 featured shell, The new Oracle Fusion middleware, WebLogic 11g supports Jython 2.2 features.

In many capacity planning analysis you need the statistcs of day or hour by hour etc., These statistics can be for JVM monitoring or for JMS or JDBC DataSource or Thread Count of a WebLogic domain.
Simply printing to standard output you can redirect them to your desired filenames which you specify in the Jython script. To do print to a file use:
f = open("JVMStat.txt", "w")
# JVM monitoasring Script loop
       print >>f, "JVM Status Line here"
And to add to the end of a existing file use:
f = open("JVMStat.txt", "a")
       print >>f, "Added lines comes here"

Hope this will give you hint for your need in WLST and file usage. For any issue keep writing back to me.

Enjoy with Jython!!! in WLST !!!


  1. Hi Pavan,

    i want to store the below script output into excel file. But i couldn't able to do it. Please help me here.

    # This script will monitor the JDBC CONNECTION POOL
    connect('user', 'pwd' ,'t3://localhost:7001')
    print ' '
    print ' '
    print ' '
    print 'Name Max Active Active WaitSecs Waiting State'
    print ' capacity Current HighCnt HighCnt Count'
    for poolRT in poolrtlist:
    pname = poolRT.getName()
    pmaxcapacity = poolRT.getAttribute("MaxCapacity")
    paccc = poolRT.getAttribute("ActiveConnectionsCurrentCount")
    pachc = poolRT.getAttribute("ActiveConnectionsHighCount")
    pwshc = poolRT.getAttribute("WaitSecondsHighCount")
    pwfccc = poolRT.getAttribute("WaitingForConnectionCurrentCount")
    pstate = poolRT.getAttribute("State")
    print '%10s %7d %7d %7d %7d %7d %10s' % (pname,pmaxcapacity,paccc,pachc, pwshc,pwfccc,pstate)
    print ' '
    print 'Error:'

  2. Hi

    Use file pointer (f) ...
    before print string.

    print '%10s %7d %7d %7d %7d %7d %10s' % (pname,pmaxcapacity,paccc,pachc, pwshc,pwfccc,pstate)

    Converting a csv file to excel is already written by Sujit Pal.
    For any doubt reply/mail

  3. Hi Pavan,

    I was trying to create csv file for the datasource details that I fetched from the Weblogic Server like ds name, targets, db url, etc. I did following:-

    print>>fileWriter, 'Data Source',',','Target',',','DB URL'

    and iterated over datasources and wrote print>>fileWriter, dsName,',',targetServerList,',',dbURL

    But, I do not find this method a clean one for creating csv file. And I think WLST doesn't supports csv module available with Python/Jython.

  4. hi pavan sir,

    actually i wanna store the output of status server script to csv file in another datasource , how to do it? can u pls help.
    thanks in advance

  5. @ Zia,

    You can get the server state using the post. The output you can customize according to your needs then you can move the stats into the file using fileObject using write or print methods explained in the above post. Hope you will try it out and share your experience.

  6. Hello Sir,
    I want to get all the user partitions including "default" under the soa_infra and store it to a variable. Please let me know how it can be done using jython through WLST. I am using weblogic 10.3.6.
    Thanks in advance.


Please write your comment here

Popular Posts

Oracle WebLogic Administration Course

Oracle WebLogic Administration Course
WebLogic Training