Skip to main content


Showing posts from September, 2014

Python: Mouse Movements using Ctypes

Dear Friends,

Below script will move the Mouse cursor in a rectangle shape. 


import ctypes
import time
for x in range(300,850,10):
    ctypes.windll.user32.SetCursorPos(x, 200)
for x in range(200,600,10):
    ctypes.windll.user32.SetCursorPos(850, x)
for x in range(850,200,-10):
    ctypes.windll.user32.SetCursorPos(x, 600)
for x in range(600,200,-10):
    ctypes.windll.user32.SetCursorPos(200, x)


1. The rectangle is not perfect as far as I know.

Python: Rename all files in a folder with names as numbers in ascending order

Hi Friends,

This script will rename files as,, 3.xls etc for all files in same folder as scripts:

import os
x = 0
for filename in os.listdir(os.path.dirname(os.path.abspath(__file__))):
    ext = filename.split('.')
    extension = ext[1]
    os.rename(filename, str(x) + '.' + extension)
    x = x + 1



1. extension = ext[1] collects the extension of file. If you skip this line, the files will be renamed without any extension.

2. Script should be in same folder as files. There should not be any folder in the folder.

Python: Generate lines taking coordinates from two lists

Dear Friends,

This will create lines while take x coordinates from list a and y coordinates from list b.

from Tkinter import *

master = Tk()
w = Canvas(master, width=200, height=100)
for x in range(1,9):
    w.create_line(int(a[x]), int(b[x]), int(a[x+1]), int(b[x+1]))


Python: Make a string translater that translate a string according to given mapping

Hi friends,

Below is the script to translate a string according to given mapping:

from string import maketrans
intab ="abcdefghijklmnopqrstuvwxyz"
trantab = maketrans(intab, outtab)
a = raw_input('Enter string to translate... ')
print b



1. Replace a with c, b with d and so on.
2. Remember, the length of intab and outtab must be same.

Python: Calculate the day of the week for any date

Dear Friends,

Below is a simple code to determine the day of the week for any given date:

import datetime
year = raw_input('Enter year in format YYYY... ')
mon = raw_input('Enter month in format MM ')
date = raw_input('Enter date in format ')
a =, int(mon), int(date)).weekday()  
if a == 0:
    print 'Its Monday'
if a == 1:
    print 'Its Tuesday'
if a == 2:
    print 'Its Wednesday'
if a == 3:
    print 'Its Thursday'
if a == 4:
    print 'Its Friday'
if a == 5:
    print 'Its Saturday'
if a == 6:
    print 'Its Sunday'

Alternatively: Use dictionary instead of too many ifs:

import datetime
year = raw_input('Enter year in format YYYY... ')
mon = raw_input('Enter month in format MM ')
date = raw_input('Enter date in format ')
a =, int(mon), int(date)).weekday()
d = { 0 : "Monday",
      1 : "Tuesday&q…

Python: Run query on a Oracle Database and save the result in a csv file

import cx_Oracle

conn_str = u'myuser/mypassword@'
con = cx_Oracle.connect(conn_str)
#print con.version
c = con.cursor()
c.execute(u'SELECT * FROM tblbooks')
with open('ans.csv', 'w') as text_file:
    text_file.write('Book Author \n')
    for row in c:
        text_file.write(row[0] + ' ' + row[1] + '\n')



1. text_file.write('Book Author \n') : This line is the fields of the table. If you don't need fields in your csv, remove it.

2. You can write with open('ans.csv', 'w') as text_file: if you want to store result in .txt file.

3. The extension cx_Oracle 5.0.2 also need to be installed.