15:FILES

15.4.1, Page number:224

In [1]:
while True:
    print "Enter Text"
    c = raw_input()
    print 'Text entered is:'
    print c
    if c[-1] == '.':
        break
Enter Text
C programming language is modern and advanced;
Text entered is:
C programming language is modern and advanced;
Enter Text
it enables addressing bits and bytes.
Text entered is:
it enables addressing bits and bytes.

15.9.1, Page number:224

In [2]:
def EOF():
    return -1
while True:
    print "Enter Text"
    c = raw_input()
    print 'Text entered is:'
    print c
    if c != EOF():
        break
Enter Text
madurai is a great city ,meenakshi temple is in the heart,millions of pilgrims visit the temple every year.
Text entered is:
madurai is a great city ,meenakshi temple is in the heart,millions of pilgrims visit the temple every year.

15.10.1,page numebr:231

In [3]:
print "Key in Text:"
f = open('myfile','w')
c=str(raw_input())
f.write(c)
print "The text is:"
f = open('myfile','r')
print f.read()
f.close()
Key in Text:
Madurai city is famous for its historical mounaments and archives,every visitor must see these places.
The text is:
Madurai city is famous for its historical mounaments and archives,every visitor must see these places.

15.12.1,page numebr:233

In [5]:
def file_output(x):
    f=open(x,"r")
    print f.read()
original = open('stud_rec','w')
copy = open('image','a')

original.write("Tirupparankundram\n")
copy.write("Tirupparankundram\n")

original.close()
copy.close()

print "After adding input,stud_rec has: "
file_output("stud_rec")
print "After append,image contains:"
file_output("image")
After adding input,stud_rec has: 
Tirupparankundram

After append,image contains:
Tirupparankundram
Tirupparankundram

15.14.1,page number:237

In [6]:
print "Type in the Text:"
f = open('madurai','w')
c=str(raw_input())
f.write(c)
print "The text read as:"
f = open('madurai','r')
print f.read()
f.close()
Type in the Text:
Madurai is an ancient city,ruled by pandyas,Tirumalai nayakar was the last prominent ruler who left many monuments; but meenakshi temple has no age
The text read as:
Madurai is an ancient city,ruled by pandyas,Tirumalai nayakar was the last prominent ruler who left many monuments; but meenakshi temple has no age

15.16.1,page number:239

In [7]:
import os
import sys
import fileinput
import string

print "Madurai Contains :"
f = open('madurai_1','r')
print f.read()
f.close()

s = open("madurai_1").read()
s = s.replace('temple', 'shrine')
f = open("madurai_1", 'w')
f.write(s)
f.close()

print "Modified File now reads :"
f = open('madurai_1','r')
print f.read()
f.close()
Madurai Contains :
Madurai is an ancient city,ruled by pandyas,Tirumalai nayakar was the last prominent ruler who left many monuments; but meenakshi temple has no age

Modified File now reads :
Madurai is an ancient city,ruled by pandyas,Tirumalai nayakar was the last prominent ruler who left many monuments; but meenakshi shrine has no age

15.18.1,page number:241

In [8]:
import re
print "Enter a stream of characters with integers embedded"
c=raw_input()
print "Integr is : ",re.sub("[^0-9]", "", c)
Enter a stream of characters with integers embedded
ab+&473.tce
Integr is :  473