How to open a PDF file in python

Hi…today i found how to open a PDF file in python..it’s so easy to open the PDF file in python..

import os
os.system(' /usr/bin/gnome-open  /home/mydirectory/filename.pdf')

output :

Thats it….Thanks….

Advertisement

How to delete files in python

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

import os
os.remove("Myfilename.txt")


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 files..here the code for this

 

import os,glob
for filePath in glob.glob("Mydirectory/Myfile*.txt")
      if os.filePath.isfile(filePath):
         os.remove(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):
            os.remove(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(user.email).downcase
    "http://gravatar.com/avatar/#{gravatar_id}.png"
  end
end

apps/views/users/index.htnl.erb

<% @users.each do |user| %>
<%= image_tag avatar_url(user) %>
<%= user.email %><%= 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

output:

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

How to download a image in a webpage in python

Hi..to all ..today 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=”http://1.bp.blogspot.com/-_l6saamIXzk/T1hikYw-bUI/AAAAAAAAD7I/073fMnOIcwQ/s640/bhavana-stills-86-473×500.jpg&#8221;# give the url of the image to download
#webbrowser.open(picture_page) # test
# open the web page picture and read it into a variable
opener1 = urllib2.build_opener()
page1 = opener1.open(picture_page)
my_picture = page1.read()
# 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”)
fout.write(my_picture)
fout.close()
# was it saved correctly?
# test it out …
webbrowser.open(filename)
# or …
# on Windows this will display the image in the default viewer
#os.startfile(filename)

Run the file by Python filename.py

output:

Thats it…Thanks…..:)

How to extract a web page in python

Hi..to 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
myurl=urllib.urlopen(“https://tuxbalaji.wordpress.com&#8221;)
html_string=myurl.read()
text= html2text( html_string )
print(text)

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

Hi..today i found in a site a small guessing game in python..

here the game codes

 

Guess.py

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

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