Tuesday, October 13, 2015

File operations in Jython

File operations are very important for Jython scripting.

File Operations in wsadmin scripting


Creating a file with list of archives which are present in the installableApp folder.

vagrant@ubuntu-was8:~/IBM/WebSphere/AppServer/installableApps$ ls > ~/scripts/applist.txt
vagrant@ubuntu-was8:~/IBM/WebSphere/AppServer/installableApps$ cat ~/scripts/applist.txt
AjaxProxy.war
CacheMonitor.ear
DefaultApplication.ear

...

WSNServicePoint.ear


These kind of need would be in every automation script; It may be list of deployables or Server to be restarted.
###############################
#
# Script File: fileoper.py
# usage : wsadmin -f [path 2="" script=""]/fileoper.py
# The file read operation
#
###############################
import os,sys

f=open("/home/vagrant/scripts/applist.txt")
for appName in f.readlines():
        print appName.split('.')[0]

print "End of the file..."


wsadmin>execfile('/home/vagrant/scripts/fileoper.py')
AjaxProxy
CacheMonitor
DefaultApplication

...
WSNServicePoint
End of the file...



The File write operations in Jython

Here we have to open the file with "w" or "w+" file mode for writing to a file object. When a file opened for write operation you can use the following :

  1. redirecting operator >>
  2. write function
  3. writelines function

vi filewr.py
# This illustrates File write operations
def cls(): print "\n"*80

# Main program starts here

cls()

f = open('/tmp/test.txt', 'w+')
writeList=["Dmgr01", \
"AppServer1", \
"AppServer2", \
"AppServer3", \
"WebServer1", \
"WebServer2", \
"WebServer3"]

print "Writing into the file..."

for s in writeList:
 print >>f, s
 print "."

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

# We can move f position here it is 0
f.seek(0)

print "After moving the file pointer to beginning..."
sno=1
for line in f.readlines():
 print sno, line
 sno+=1

f.close()


The execution of the above file write operation is as follows:



No comments:

Post a Comment

Containerization with Docker

Containerization with Docker
Learn modern microservices containers grow