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

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

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

Leave a Reply