How to delete files in python

Hi…to all today i learnt how to delete files in the codes

import os

suppose if you want to delete all the file named myfile1.txt,myfile2.txt…it goes on like this.. we can use myfile*.txt  to delete all those the code for this


import os,glob
for filePath in glob.glob("Mydirectory/Myfile*.txt")
      if os.filePath.isfile(filePath):

if you want to delete all the files in subdirectories you can use the following code

import os, glob
for root, dirs, files in os.walk('MyDirectory'):
    for filePath in glob.glob(os.path.join(root, "MyFile*.txt")):
        if os.path.isfile(filePath):

it is possible to delete files you didn’t intend to. So make sure you test thoroughly before implementing code like this..

Thats it…thanks….happy coding with python…:)

How to add Gravatar to the user in Ruby on Rails

HI…to all today i learnt how to set Gravator to the user.. Lets create a rails project like this.. $ rails new gravator

$ cd gravator

now create a scaffold for the gravator by

$ rails g scaffold users avator email:text

now migrate db and run server

$ rake db:create

$ rails s 

now open browser and add few users localhost:3000/users

now we have to add gravatar to the user..

open app/helper/application_helper.rb

module ApplicationHelper
  def avatar_url(user)
    gravatar_id = Digest::MD5::hexdigest(


<% @users.each do |user| %>
<%= image_tag avatar_url(user) %>
<%= %><%= link_to 'Show', user %><%= link_to 'Edit', edit_user_path(user) %><%= link_to 'Destroy', user, :method => :delete, :data => { :confirm => 'Are you sure?' } %>
<% end %>

Now run the server by rails s


Thats it…now we added gravatar to the user…Thanks …

How to download a image in a webpage in python all i learnt how to download a image in a webpage in python…here the code is..

we need to copy the url of the image and give that in our code ..

#! usr/bin/python
import urllib2
import webbrowser
import os
# find yourself a picture on a web page you like
# (right click on the picture, look under properties and copy the address)
picture_page=”×500.jpg&#8221;# give the url of the image to download # test
# open the web page picture and read it into a variable
opener1 = urllib2.build_opener()
page1 =
my_picture =
# open file for binary write and save picture
# picture_page[-4:] extracts extension eg. .gif
# (most image file extensions have three letters, otherwise modify)
filename = “my_image” + picture_page[-4:]
print filename # test
fout = open(filename, “wb”)
# was it saved correctly?
# test it out …
# or …
# on Windows this will display the image in the default viewer

Run the file by Python


Thats it…Thanks…..:)

How to extract a web page in python all today i learnt have to extract a webpage in python …here is the simple code…it’s so easy.. we need to install the following..

$ sudo apt-get install python-setuptools
$ sudo easy_install stripogram

import urllib
from stripogram import html2text
text= html2text( html_string )

This is will print the source page of the given url as text as our output..

Thanks…enjoy coding in python….:)

Small game in python i found in a site a small guessing game in python..

here the game codes

  1. # This is a guess the number game.
  2. import random
  3. guessesTaken = 0
  4. print(‘Hello! What is your name?’)
  5. myName = input()
  6. number = random.randint(1, 20)
  7. print(‘Well, ‘ + myName + ‘, I am thinking of a number between 1 and 20.’)
  8. while guessesTaken < 6:
  9.     print(‘Take a guess.’) # There are four spaces in front of print.
  10.     guess = input()
  11.     guess = int(guess)
  12.     guessesTaken = guessesTaken + 1
  13.     if guess < number:
  14.         print(‘Your guess is too low.’) # There are eight spaces in front of print.
  15.     if guess > number:
  16.         print(‘Your guess is too high.’)
  17.     if guess == number:
  18.         break
  19. if guess == number:
  20.     guessesTaken = str(guessesTaken)
  21.     print(‘Good job, ‘ + myName + ‘! You guessed my number in ‘ + guessesTaken + ‘ guesses!’)
  22. if guess != number:
  23.     number = str(number)
  24.     print(‘Nope. The number I was thinking of was ‘ + number)



run the file by python

its nice to learn python by playing with small games like this..