Archive for the ‘Python’ Category

Chapter 17 – Python High Level File I/O Operations

 
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
 
import shutil
 
print "\n————————————\nChapter 17 – High level File I/O \n————————————\n"
 
s = shutil
 
s.copy("data1","data5")
 
print "File data1 copied to data5 -shutil.copy() \n"
 
s.copy2("data1","data6")
 
print "File data1 copied to data6 without changing timestamp -shutil.copy1() \n"
 
s.move("data5","temp/data7")
 
print "File data5 moved to temp/data5 -shutil.move() \n" 
 
srcdir = "temp"
desdir = "temp2"
 
s.rmtree(desdir)
 
print "Remove the whole directory tree -shutil.rmtree() \n"
 
s.copytree(srcdir,desdir)
 
print "Copy the whole directory tree -shutil.copytree() \n"
 
print "\n—————————————-"
 
#END

Read more »

Chapter 16 – Python Functions

 
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 16 – Functions \n————————————\n"
 
def sum_mul(a,b):
 return a+b,a*b
 
c,d = sum_mul(10,20)
 
print "Sum = %d and Product = %d" %(c,d) 
 
print"—————————————-"
 
#END

Read more »

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

Read more »