Sunday, 2 August 2015

Lets Write some scripts using Python

Magic 8ball Script



#! /usr/bin/env python
import sys,re,random

while True:
      x = raw_input("Please ask a question: ")
      n = random.randint(1, 7)
      if re.match("^exit$|^close$", x):
            print "GoodBye!"
            sys.exit()
      elif n == 1:
            print "The answer lies in your heart"
      elif n == 2:
            print "I do not know"
      elif n == 3:
            print "Almost certainly"
      elif n == 4:
            print "No"
      elif n == 5:
            print "Why do you need to ask?"
      elif n == 6:
            print "Go away. I do not wish to answer at this time."
      elif n == 7:
            print "Time will only tell"


Finding Specified Files


Here am searching all mp3 files in system.

import fnmatch
import os
rootPath = '/'
pattern = '*.mp3'
for root,dirs,files in os.walk(rootPath):
      for filename in fnmatch.filter(files,pattern):
            print(os.path.join(root,filename))


Date and Time Script



from datetime import datetime
now = datetime.now()
mm = str(now.month)
dd = str(now.day)
yyyy = str(now.year)
hour = str(now.hour)
mi = str(now.minute)
ss = str(now.second)
print mm + "/" +dd + "/" +yyyy +" " + hour + ":"+mi +":"+ss

No comments:

Post a Comment