File - 157998467 - 1688528332 - Text File Practice Questions - 1
File - 157998467 - 1688528332 - Text File Practice Questions - 1
F=open( )
Only using file object ( major specifier/ variable) can we open the file and read
and write, create, extract, append, etc.
3) What are the advantages of saving data in : (i) binary form (ii) text form ?
Absolute path is the location of file from root directory. Relative path stored
in default file location/directory.
5) What is the difference between seek() and tell() method? Explain via example
seek goesto desired cursor position. Tell prints current cursor position, seek takes 3
variables etc
8) Write statements to open a binary file C:\Myfiles\Textl.txt in read and write mode
by specifying the file path in two different formats.
f=open(C:\Myfiles\”Textl.txt”,’r’)---- absolute
f= open(“Textl.txt”,’w’) -- relative
Method Description
write(s) Writes the string s to the file and returns the number characters
written.
10)Observe the following code and answer the questions that follow:
File = open("Mydata","a")
File.close()
ii. Fill the Blank 1 with statement to write “ABC” in the file “Mydata”
f.write ("0123456789abcdef")
f.read(5) – no ouptut
f = open("a.txt", 'w')
f.write(line1)
f.write(line2)
f.close()
f = open("a.txt", 'r')
text = f.read()
f.close()
14)Replace word
data = fin.read()
Arsha Vidya Mandir
Lecture Notes -2023-24
data = data.replace(‘my', ‘your') – data is the string of content in file. So replace is
the string function that replaces my with your.
fin.close()
fin.write(data)
fin.close()
f=open("dairy.txt","w")
line1="jjjjjvnvrrrrjr"
f.write(line1)
line2="\nefeeeehjybv"
f.write(line2)
f.close()
f=open("dairy.txt",'r')
c=0
x=f.readlines()
for i in x:
c+=1
print(c)
f.close()
17)Write a program to count the words “this” and “these” present in a text file
“Poem.txt”
Arsha Vidya Mandir
Lecture Notes -2023-24
18)Write a program to count the words “to” and “the” present in a text file
“Poem.txt”.
f=open("poem.txt","w")
f.write(line1)
f.write(line2)
f.close()
Arsha Vidya Mandir
Lecture Notes -2023-24
f=open("dairy.txt",'r')
c=0
x=f.read()
for i in x:
if i=='o':
c+=1
print(c)
f.close()
20)Write a Python Program to Read a Text File and Print all the Numbers Present in
the Text File.
f=open("Article.txt",'w')
f.write("aAb1234556789600BCccDD")
f.close()
f=open("Article.txt",'r')
c=0
ch=f.read()
for i in ch:
if i.isdigit():
c=c+1
print(c)
f.close()
21)Write a program to count the number of upper- case alphabets present in a text file
“Article.txt”.
Arsha Vidya Mandir
Lecture Notes -2023-24
f=open("Article.txt",'w')
f.write("aAbBCccDD")
f.close()
f=open("Article.txt",'r')
c=0
ch=f.read()
for i in ch:
if i.isupper():
c=c+1
print(c)
f.close()
22)Write a Python Program to Count the Number of Blank Spaces in a Text File.
if ' ' in i:
23)Write a Python Program to Read a File and Capitalize the First Letter of Every
Word in the File.
file=open('Demo.txt')
f=(file.read()).split()
for i in f:
print(i[0].upper(),i[1:],sep="",end=" ")
f1=open('demo4.txt')
x=f1.read()
print(x[-1::-1])
25)Write a method in python to read the content from a text file diary.txt line by line
and display the same on screen.
Arsha Vidya Mandir
Lecture Notes -2023-24
f=open("dairy.txt","r")
x=f.readlines()
for i in x:
print(i)
f.close()
26)Write a python program to print the last two lines of a file.
f1=open('demo4.txt')
y=f1.readlines()
print(y[-1],y[-2])
27)Program in python to show word with maximum length from a text file.
f=open("d:\\a.txt","r")
lword=''
for t in f.readlines():
for r in t.split():
if len(r)>len(lword):
lword=r
f.close()
28)Python program to combine each line from first file with the corresponding line in
second file.
f=open("a.txt","w")
g=open("b.txt","w")
f.write("Helllo")
g.write("Students")
Arsha Vidya Mandir
Lecture Notes -2023-24
f.close()
g.close()
f=open("a.txt","r")
g=open("b.txt","r")
print(line1+line2)
print(line1+' '+line2)
F=open(“covid.txt”)
Str1=---------
Str2=----------
Str3=----------
Arsha Vidya Mandir
Lecture Notes -2023-24
30)Complete the following.
F=open(“corona.txt”)
for---I in f.read()--------
print-(i)-----------
31)F=open(“covid.txt”)
Print(f.read(2))—first 2 chaer
34)Python Program to read character by character from a text file. (DOES NOT
MEAN PRINTING CHARACTER BY CHARACTER, AT A TIME, READS
ONLY ONE CHARACTER. EG :BARATH, B READ FIRST ,THEN AFTER
NEXT ITERATION, READS A ETC)
char = file.read(1)
if not char:
break
else:
print (char),
file.close()
Arsha Vidya Mandir
Lecture Notes -2023-24
f=open("a.txt","w")
f.close()
f=open("a.txt","r")
c=0
for t in f.readlines():
for r in t.split():
if(len(r)==2):
c=c+1
f.close()
Len(f.read())
38) Polina Raj has used a text editing software to type some text in an article. After
saving the article as MYNOTES.TXT, she realised that she has wrongly typed
alphabet K in place of alphabet C everywhere in the article. Write a function
Arsha Vidya Mandir
Lecture Notes -2023-24
definition for PURETEXT() in Python that would display the corrected version of
the entire article of the file MYNOTES.TXT with all the alphabets ――K‖‖ to be
displayed as an alphabet ――C‖‖ on screen. Note : Assuming that
MYNOTES.TXT does not contain any C alphabet otherwise.
39)Write a program that appends / copies the contents of one file to another.
40)Write a program that reads characters from the keyboard one by one. All lower
case characters get stored inside the file LOWER, all upper case characters get
stored inside the file UPPER and all other characters get stored inside file
OTHERS.
41)Read a text file and display the number of vowels/ consonants/ uppercase/
lowercase characters in the file.
42)Remove all the lines that contain the character `i' in a file and write it to another
file.
44)Write a function in Python to read the content of a text file “Mumbai.TXT” and
display all those lines on screen, which are either starting with ‘M’ or starting with
‘i’.
45)Write a program to count the words “is” and “was” present in a text file
“Poem.txt”.
46)Write a program to count the number of lower- case alphabets present in a text file
“Article.txt”.
51)Write a program that reads characters from the keyboard one by one. All lower
case characters get stored inside the file LOWER, all upper case characters get
stored inside the file UPPER and all other characters get stored inside file
OTHERS.
52)Write a function in Python to count and display the number of lines starting with
alphabet ‘M’ and ending with alphabet ‘I’ present in a text file ” LINES.TXT”.
53)Read a text file and display the number of vowels/ consonants/ uppercase/
lowercase characters in the file.
54)Remove all the lines that contain the character `a' in a file and write it to another
file.
56)Write a function in Python to read the content of a text file “DELHI.TXT” and
display all those lines on screen, which are either starting with ‘D’ or starting with
‘M’.
57)Write function definition for COUNTNU( )in Python to read the content of a text
file CONTENT.TXT, count the characters N and U (in lower case as well as
upper case) present in the file.
f=open("d:\\a.txt","a")
f.write(s)
Arsha Vidya Mandir
Lecture Notes -2023-24
f.close()
59)Write a program that appends / copies the contents of one file to another.
with open("Article.txt") as f:
with open("Dairy.txt", "w") as f1:
for line in f:
f1.write(line)
60)Write a function in Python to count and display the number of lines starting with
alphabet ‘A’ present in a text file ” LINES.TXT”. e.g., the file “LINES.TXT”
contains the following lines :
def func(l):
c=0
for line in l:
if line[0] == ‘A’
c += 1
return c
f1 = open(‘LINES.txt’, ‘r’)
l = f1.readlines()
*****