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..

How to Connect MySQL database with Python

Hi…to all today i’m going to tell how to connect python with Mysql

we need a package named MySQLdb

install that package inorder to connect mysql and python.

$ sudo apt-get install python-mysqldb

Here is the code to connect to mysql

# !usr/bin/python
import MySQLdb
#creating connection using Mysqldb and mqsql dbname,username and password
con = MySQLdb.connect(host = “localhost”, user = “root”, passwd = “”, db = “balaji”)
#creating currsor to access to db
#Executing query using cursor
cursor.execute(“SELECT VERSION()”)
#printing the result
print result
#closing the db connection .

Save this file with .py extension

now run the file by $ python

Thats it..enjoy the coding with python…happy times!!!

Bootstrap Twitter in Rails

Hi…to all to day i learned Bootstrap from Twitter in rails application…

Before getting into session first ,let we  know about twitterbootstrap..It helps to build beautiful web applications..It provides variety of CSS and Javascripts for  making layouts,navigations.lot more etc..

Now, we ll create a simple application to store the products and price in a shop and then add TwitterBootstrap..

we ll call the app as store and create scaffold named Product model to do some work

$ rails new store

$ cd store

$ rails g scaffold product name:string price:decimal –skip-stylesheets

$ rake db:migrate

Note: we skipped stylesheets because we are going to use Bootstrap’s stylesheets,css,etc..

now you can see the page as like this..

now.we have to add Bootstrap to this application..

open Gem file

  1. # Gems used only for assets and not required  
  2. # in production environments by default.  
  3. group :assets do  
  4.   gem ‘sass-rails’,   ‘~> 3.2.3’  
  5.   gem ‘coffee-rails’, ‘~> 3.2.1’  
  7.   # See for more supported runtimes  
  8.   # gem ‘therubyracer’  
  10.   gem ‘uglifier’, ‘>= 1.0.3’  
  11.   gem ‘twitter-bootstrap-rails’  
  12. end

now bundle it and install Bootstrap Twitter

$ bundle install

$ rails g bootstrap:install

this file will be installed in assests/bootstrap_and_overwrites_css.less

now if yuo run the server u can feel difference ..Now our page looks like this…


now we can improve our layout by bootstrap..


add <div class=”container”>  in body of the file.

now we are going to add sidebar to our page


    <div><%= yield %></div>
      <h2>About Us</h2>
      <p>This is a product based company.Here we are generating a very good products for pepole in a quality manner. Our products are most popular in the market.</p>	

now you can see the about us as sidebar in our app like this

Finally we are going to add the navigation bar such as somestore,contact us..etc

add these lines on top of the body of the file /app/views/layouts/application.html.erb

  • <div class=”navbar navbar-fixed-top”>  
  •   <div class=”navbar-inner”>  
  •     <div class=”container”>  
  •       <a class=”btn btn-navbar” data-toggle=”collapse” data-target=”.collapse”>  
  •         <span class=”icon-bar”></span>  
  •         <span class=”icon-bar”></span>  
  •         <span class=”icon-bar”></span>  
  •       </a>  
  •       <a class=”brand” href=”#”>Some Store</a>  
  •       <div class=”nav-collapse”>  
  •         <ul class=”nav”>  
  •           <li><%= link_to “Browse Products”, products_path %></li>  
  •           <li><%= link_to “Price List” %></li>  
  •           <li><%= link_to “Contact Us” %></li>  
  •           <li><%= link_to “Cart” %></li>  
  •         </ul>  
  •       </div>  
  •     </div>  
  •   </div>  
  • </div>  

now if you run the server you can see the navigations bar in our application…

Final Tweaks to The Header

Our application’s layout file is pretty much complete now but there are a couple of thing we need to add in the head section to ensure that it works everywhere.


  <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode/svn/trunk/html5.js" type="text/javascript"></script>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

the above code for HTML5 support and mobile behaviour .

Next we’ll use Twitter Bootstrap to improve the look of this page. Instead of walking through each change manually we’ll use one of the generators provided by the gem.

$ rails g bootstrap:themed products -f

now run server by $ rails s

after adding products and price to our store apllication ..Our application looks like this…

Thats it…Thanks to all….happy times!!!!!

Sending emails using gmail in Rails

Hi…to all today i learned how to send emails using gmail in rails

here are the steps

Create application using

$ rails new mailit

change the directory using      $ cd   /mailit

next create the scaffolder name User with name and email

$ rails g scaffold user name:string email:string

then run the database migrations

$ rake db:migrate

next we have to do configuration settings for gmail

create a file in  config/initializers named setup_mail.rb  

ActionMailer::Base.smtp_settings = {
:address => “”,
:port => 587,
:domain => “”,
:user_name => “”,
:password => “yourpassword”,
:authentication => “plain”,
:enable_starttls_auto => true

now we completed the configuration ,now generate the mailer using

$ rails g mailer user_mailer

this will create the user_mail.rb in  /apps/mailer

now add the following code in to that file

  1. class UserMailer < ActionMailer::Base  
  2.   default :from => “”  
  4.   def registration_confirmation(user)  
  5.     mail(:to =>, :subject => “Registered” , :body =>”Thanks for registering”)  
  6.   end  
  7. end

create textfile named regisetration_confirm.text/erb in apps/views/user_mailer

Thanks for registering!!!

Add the following line to the  /app/controllers/users_controller.rb


def create
@user =[:user])

respond_to do |format|
UserMailer.registration_confirmation(@user).deliver # added line 
format.html { redirect_to @user, :notice => ‘User was successfully created.’ }
format.json { render :json => @user, :status => :created, :location => @user }
format.html { render :action => “new” }
format.json { render :json => @user.errors, :status => :unprocessable_entity }

Thats it rin the server by $ rails server 

then give name and emailid to which you have to send email..

then click create button ,now you will see like this

now go to your mail and check your inbox

Thanks to all…happy times!!!!