Archive for the ‘Python’ Category

Chapter 14 – Python File Write 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 14 – File Writing \n————————————\n"
 
h1 = open("data1","r")
h2 = open("data2","w")
h3 = open("data3","w")
print h1
 
h1 = open("data1","r")
s = h1.readlines()
print "\nReading the Content – readlines() : ",s
 
for i in s:
 h2.write(i)
 
print "File contents are written to data2 using write()"
 
h3.writelines(s)
print "File contents are written to data3 using writelines()"
 
print "\n————————————–\n"
 
h1.close()
h2.close()
 
#END

Read more »

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

Read more »

Chapter 12 – Python While Loop

 
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 12 – While Loop \n————————————\n"
 
c=1
while(c!=0):
 i = input("Enter an integer : ")
 while(i > 0):
  print i
  i -= 1
 c = input("To quit press 0 : ")
 
print "\n————————————–\n"
 
#END

Read more »