Chapter 13 – Python File Read 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
 
print "\n————————————\nChapter 13 – File Reading \n————————————\n"
 
h1 = open("data1","r")
print h1
 
print "\nReading the first line -readline() : ",h1.readline()
 
h1 = open("data1","r")
print "\nReading the full content – read() : \n",h1.read()
 
h1 = open("data1","r")
print "\nReading the First 25 Character – read(25) : ",h1.read(25)
 
h1 = open("data1","r")
print "\nReading the Content – readlines() : ",h1.readlines()
 
print "\n————————————–\n"
 
#END

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

 

python-file-reading-13
 

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

Leave a Reply