Chapter 15 – Python Exceptions

 
Step 1
 
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
 
#!/usr/bin/python
#Author : Jijo K Jose
 
print "\n————————————\nChapter 15 – File Writing \n————————————\n"
 
while 1:
 f1=raw_input("Enter the file name : ")
 try:
  h1 = open(f1,"r")
  print "\nFile contents are :\n",h1.read()
  break 
 except:
  print "Problem in opening file : ",f1
 
print"—————————————-"
 
#END

 
Step 2
 
Change file permission to executable using chmod command.
 
Step 3
 
Execute the python file.
 
python-exceptions-15
 

 
You can leave a response, or trackback from your own site.

Leave a Reply