April 19, 2017

Python script for file handling - Read & Write

# Basic File Handling in Python - Read & Write
# coding: utf-8 # for Non-ASCII character support

# Writing to a file
f = open("<file path>/file_read_write.txt", "w");
f.write("Name                    Age                  \n")
f.write("-----------------------------------------\n")

mystring = "%s                   (%d) \n" % ("Ram", 32)
f.write(mystring)

f.close() 

# Reading from a file
f = open("<file path>/file_read_write.txt", "r") 
print f.read() 

f.close()

Note:
you may also try the following commands for selective read(s)
    print f.read(4)
    print f.readline()
    print f.readline(3)

April 6, 2017

How to validate an email address using Python Script?

Code snippet to validate the email id taken as an input via command line

# Python script to validate input email address
# for system input
import sys
print(sys.argv)

# for regular expression
import re
print "\n" 

email_id = raw_input("Please enter your email id: ")
if re.match("[^@]+@[^@]+\.[^@]+", email_id):
print "Hurray! you have entered a valid email id: ", email_id
else:
print "Better luck next time with a valid email id"

March 15, 2017

How to upload files to S3 using Python Boto3?

The below script gives you a quick example of uploading a specific file to the S3 bucket, provided the IAM user calling the API has the s3:GetObject permissions on the S3 bucket.

# Boto3 script to upload a file to S3
import boto3
session = boto3.Session(profile_name='<profilename>') #ensure to use appropriate  profile
s3_client = session.client('s3')

print "Upload the your test file to S3"
s3_client.upload_file('<file path>/<file name with extension>', '<s3 bucket name>', '<prefixfolder/filename with extension>)

print "Success! Upload complete"
print "Getting list"
files=s3_client.list_objects(Bucket='<s3 bucket name>', Prefix='<prefixfolder/filename with extension>')['Contents']

for key in files:

   print key

December 27, 2016

Guest Lecture on `Industry Awareness` @ BIT Sathy

It is the time of the year again to visit my Alma Mater to address the highly energetic and enthusiastic final year Engineering students from Computer Science department upon the invite from our revered head of the department, Dr. Thangaraj.

The lecture was on Industry Awareness covering 3 major aspects to prepare the budding engineers for the onward journey when they set their foot loose from the compassionate college life to the competitive corporate world.

1. What does the Industry Expect from the emerging Engineers?
2. The other side of the coin for the Engineers. 
3. When do you quench your Entrepreneurial thirst? 

Followed by much involved students interaction and all is to inspire our juniors to dream more, to learn more and to do more in-turn to the society.


Guest Lecture @ BIT Sathy

Refer the link to certificate of appreciation  from the college for the Guest Lecture.

December 1, 2014

Gradle script to list xml files recursively from specified directory

In the previous article, we saw how to list xml files recursively from the specified directory using Ant Script.

Here is the code snippet to do the same in Gradle script.

import groovy.io.FileType

// defining defaulttasks
defaultTasks 'listfile'

task listfile ()<< {
// user defined property - srcdir
ext.srcdir = "D:\Src"
println ' '
}
listfile.doFirst {
    print '-----------------------------------------'
}

listfile << {
def list = []
def dir = new File(listfile.srcdir)
dir.eachFileRecurse (FileType.FILES) { file ->
  if(file.name.endsWith('.xml')) {
    list << file
}
}
println 'List of xml files: \n-----------------------------------------'
list.each {
  println it.path
}
 println '-----------------------------------------'
}