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….
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…:)
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 …
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”# 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…..:)
Hi …to all today i learnt how to get the date and time in python..
here the codes are…
>>> from time import gmtime, strftime
>>> strftime(“%Y-%m-%d %H:%M:%S”, gmtime())
‘2009-01-05 22:14:39
for more Direct time modulation http://docs.python.org/library/time.html#time.strftime
Thats it..Thanks …
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”)
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….:)
Hi..today i found in a site a small guessing game in python..
here the game codes
Guess.py
run the file by python Guess.py
its nice to learn python by playing with small games like this..