Text File Programs Xii C
Text File Programs Xii C
For opening a file to read some data from it, use ‘r’ mode.
Then use read(), readlines() or readline() method to read data from it.
For creating a file and write something to it use ‘w’ or ‘a’ mode.
Then use write() or writelines() method to write data into it.
To add some more lines to an existing file without losing the previous
contents, use ‘a’ mode. (Append mode)
====================================================================
#Write a function RevText() to read a text file "Story.txt" and Print only word
starting with 'I' in reverse order. Example: If value in text file is: INDIA IS MY
COUNTRY Output will be: AIDNI SI MY COUNTRY
def revtext():
f=open("sorty.txt","r")
s=""
while True:
d=f.readline()
if not d:
break
else:
m=d.split()
for i in m:
if i[0]=='i' or i[0]=='I':
s=s+" "+(i[::-1])
else:
s=s+" "+i
print(s)
s=""
revtext()
============================================================
#To print the digits in a file ( CHARACTER BY CHARACTER READING)
def fileread():
f =open("d:\\xiic\\test.txt",'r')
str=f.read()
for i in str:
if i.isdigit():
print("\nDigits are=",i)
f.close()
fileread()
================================================
#To print some characters from file
f =open("d:\\xiic\\test.txt",'r')
str=f.read(4)
print(str)
=================================================