Friday, 24 January 2014

Executing a OS Commands-copy,delete,extract tar using Python

Executing a OS Commands-copy,delete,extract tar using Python
'''
Created on 24 Jan 2014

@author: Xavier Adaickalam
@note: execute_caommand.py
'''
import os
import shutil
import tarfile
import glob
import subprocess

def delete_files(ljs_dir):
    print('Deleting Files...')
    delete_directory(ljs_dir +'/*')
    print('Deleting Files...DONE')

def delete_directory(dir_name):
    print("Deleting " + dir_name)
    files = glob.glob(dir_name)
    for f in files:
        if os.path.isdir(f):
            shutil.rmtree(f)
            print('Deleted Directory :'+f)
        else:
            os.remove(f)
            print('Deleted File :'+f)

def extract_tar(filename,to_dir):
    print('Extracting :'+filename + ' to ' + to_dir)
    tfile = tarfile.open(filename,'r') 
    for tarinfo in tfile:
        #print(tarinfo.name)
        tfile.extract(tarinfo,to_dir)             
    #tfile.exctractall()    
    tfile.close()
    print('Completed Extraction :'+filename)
    
def copy_file(from_dir,to_dir):
    files = glob.glob(from_dir + '/*.war')
    for f in files:
        print('Copying file :' + f + ' to :' +to_dir)
        shutil.copy(f, to_dir)

def run_command(cmd,directory):
    subprocess.check_call(cmd,cwd=directory)
    
if __name__ == '__main__':
    cmd1 = [ 'ls', '-a']
    run_command(cmd1,'.')

Generating a CSV content using Python

Python has a library module called csv which can be used to generate a sample data in csv format.
'''
Created on 24 Jul 2013

@author: Xavier Adaickalam
@note: genetate_data_csv.py
'''
import csv

def generate_data(): 
    try:
        f = open('data.csv', 'wb')
        data_set = []
        data_props = ['ID','NAME','FIRST_NAME','LAST_NAME','MIDDLE_NAME','NICK_NAME','PROFILE','PICTURE',\
                    'WEBSITE','EMAIL', 'EMAIL_VERIFIED', 'GENDER', 'DATE_OF_BIRTH', 'ZONEINFO', \
                    'LOCALE','PHONE_NUMBER','STREET_ADDRESS','CITY','REGION','COUNTRY','POSTAL_CODE',\
                    'MARITAL_STATUS']
        try :
            contentwriter = csv.writer(f,delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
            for i in range(100):
                for a in data_props:
                    data_set.append(a + '-'  + str(i))
                contentwriter.writerow(data_set)
                data_set=[]
        finally:
            f.flush()
            f.close();
    except IOError:
        pass
if __name__ == '__main__':
    generate_data()