TEXT FILE
1. Write a method in Python to read the content from a text file [Link]
line by line and display the same on the screen.
[Link]
Mountains high with peaks so steep,
Where echoes of the wild leap.
Nature's beauty, pure and true,
In every moment, ever new.
PROGRAM
def read():
f=open("[Link]","r")
while True:
s=[Link]()
if s=='':
break
else:
print(s, end='')
[Link]()
read()
OUTPUT
Mountains high with peaks so steep,
Where echoes of the wild leap.
Nature's beauty, pure and true,
In every moment, ever new.
2. Write a method in Python to read the lines from a text file [Link] and
display those lines which are starting with an alphabet F.
[Link]
forest's peace, they roam and play,
A bond that grows stronger every day.
Together in this woodland fair,
Dog and forest, a perfect pair.
PROGRAM
def read():
f=open("[Link]","r")
lines=[Link]()
for i in lines:
if i[0]=='F' or i[0]=='f':
print(i)
[Link]()
read()
OUTPUT
forest's peace, they roam and play,
3. Write a method in Python to read the lines from a text file [Link] and
display the count of lines which are starting with an alphabet F.
[Link]
forest's peace, they roam and play,
A bond that grows stronger every day.
Together in this woodland fair,
Dog and forest, a perfect pair.
PROGRAM
def read():
f=open("[Link]","r")
count=0
lines=[Link]()
for i in lines:
if i[0]=='F' or i[0]=='f':
count+=1
print("The number of lines starting with the alphabet F is:", count)
[Link]()
read()
OUTPUT
The number of lines starting with the alphabet F is: 1
4. Write a method in Python to read the lines from a text file [Link] and
display those lines which are starting with an alphabet F or D.
[Link]
forest's peace, they roam and play,
A bond that grows stronger every day.
Together in this woodland fair,
Dog and forest, a perfect pair.
PROGRAM
def read():
f=open("[Link]","r")
lines=[Link]()
for i in lines:
if i[0]=='F' or i[0]=='f' or i[0]=='D' or i[0]=='d':
print(i,end='')
[Link]()
read()
OUTPUT
forest's peace, they roam and play,
Dog and forest, a perfect pair.
5. Write a method in Python BigLines() to read the lines from a text file
[Link] and display those lines which are bigger than 50 characters.
[Link]
Mountains high with peaks so steep, Where echoes of the wild leap.
Nature's beauty, pure and true,
In every moment, ever new.
PROGRAM
def BigLines():
f=open("[Link]","r")
lines=[Link]()
for i in lines:
if len(i)>50:
print(i,end='')
[Link]()
BigLines()
OUTPUT
Mountains high with peaks so steep, Where echoes of the wild leap.
6. Differentiate between text file and binary file.
TEXT FILE BINARY FILE
Text files store information in Binary files are used to store
ASCII characters. binary data such as images, audio,
video and text.
Each line of text is terminated with There is no delimiter in Binary
a special character known as EOL File.
(End of Line)
Text files are easy to understand Binary files are difficult to
because these files are in human understand
readable form.
7. Differentiate between r+ mode and w+ mode.
r+ mode w+ mode
Used for both reading and writing. w+ mode is also used for both
reading and writing.
In r+ mode the file pointer is at the In w+ mode the file pointer is at
beginning of the file the end of the file
If the file does not exist, it will If the file does not exist, it will
display the FileNotFound error. create the new file
If the file exist, it does not show If the file exist, it will write data
any error. in the file(overwriting the
previous content)