0% found this document useful (0 votes)
239 views14 pages

File - 157998467 - 1688528332 - Text File Practice Questions - 1

Uploaded by

Rakesh S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
239 views14 pages

File - 157998467 - 1688528332 - Text File Practice Questions - 1

Uploaded by

Rakesh S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Arsha Vidya Mandir

Lecture Notes -2023-24

Class:XII - A Subject: Computer Science

Topic: Text File Practice Questions -1 Teacher: B.Madasamy

1) What is the difference between “w” and “a” modes?

W mode overwrites text during second execution of code whereas, a mode


appends/add data at the end of the file.

2) What is the significance of file-object?

F=open( )

F is the file object.

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 ?

i) Binary form increases security as text is stored in 0s and 1s – encrypted


form
ii) Text file is convenient to read as ascii characters are present

4) What is the difference between relative path and absolute path?

D://Path/ Demo/File/”hi.txt” - Absolute path / Relative Path

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

6) What is the difference between Remove() rename() .


Arsha Vidya Mandir
Lecture Notes -2023-24
7) Nancy intends to position the file pointer to the beginning of a text file. Write
Python statement for the same assuming F is the File object. f.tell() f.seek(0,0)

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

9) Write() and writelines()How is method write() different from writelines() in


python?

Method Description

write(s) Writes the string s to the file and returns the number characters
written.

writelines(s) Writes all strings in the sequence s to the file.

10)Observe the following code and answer the questions that follow:

File = open("Mydata","a")

File.write(‘ABC’)______________________ #Blank1 File.write(“ABC”)

File.close()

i. What type (Text/Binary) of file is Mydata?

ii. Fill the Blank 1 with statement to write “ABC” in the file “Mydata”

11)Differentiate between the following :

(i) f = open(‘diary.txt’, ‘r’)

(ii) f = open(‘diary.txt’, ‘w’)

 Read is the default mode, it need not be specified


Arsha Vidya Mandir
Lecture Notes -2023-24
 Read raises error if file doesn’t exist. Write mode wont raise error if file
doesn’t exist/

12)Find the output:

f = open ("mytry.txt", "w+")

f.write ("0123456789abcdef")

f.read(5) – no ouptut

f.seek (5,0) – cursor goes to fifth char from first

print(f.read(2)) – “56” – cursor is after 5th char

13)Line by Line code.

f = open("a.txt", 'w')

line1 = 'Welcome to python'

f.write(line1)

line2="\nRegularly visit HLP"

f.write(line2)

f.close()

f = open("a.txt", 'r')

text = f.read()

print(text) ------- Welcome to python

Regularly visit HLP

f.close()

14)Replace word

fin = open("dummy.txt", "rt")

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 = open("dummy.txt", "wt")

fin.write(data)

fin.close()

15)Write a python program to count the number of words in a file.

16)Write a python program to count the number of lines in a file.

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”.

file = open("Article.txt", "w")


file.write("The school to the the The ")
file.close()
to=0
the=0
file = open("Article.txt", "r")
lst = file.readlines()
for i in lst :
word = i.split()
for j in word :
if j == "to" :
to += 1
elif j == "the" or j == "The" :
the += 1
print("Number 'to' : " , to)
print("Number 'the' : " , the)
file.close()
19)Write a Python Program that Reads a Text File and Counts the Number of Times a
Certain Letter Appears in the Text File.

f=open("poem.txt","w")

line1="Hello Students Hellooooooo"

f.write(line1)

line2="\nHow are yoooooou"

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:

same code otherwise

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=" ")

24)Write a Python Program to Read the Contents of a File in Reverse Order.

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

print("maximum length word is ",lword)

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")

for line1, line2 in zip(f,g):

print(line1+line2)

COULD ALSO BE WRITTEN AS :

for (line1, line2) in zip(f,g):

print(line1+' '+line2)

29)Find the output:

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

Print(f.read(2) – next 2 char

Print(f.read()) – prints all the REST characters

32)To count the no. Of lines begins with upper case.

33)To search the words.

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)

file = open('demo.txt', 'r')

while 1: OR While True:

char = file.read(1)

if not char:

break

else:

print (char),

file.close()
Arsha Vidya Mandir
Lecture Notes -2023-24

35)Display all the words contains 2 characters.

f=open("a.txt","w")

f.write("I am an hoe are you")

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

print("no of words in file are ",c)

f.close()

36)Display the size of the file.

Len(f.read())

37)Define flush() method.

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.

43)Write function definition for WORD3CHAR() in Python to read the content of a


text file FUN.TXT and display all those words, which has three characters in it.

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”.

47)What is the purpose of PYTHONPATH environment variable?

48)What is pickling and unpicking?


Arsha Vidya Mandir
Lecture Notes -2023-24
49)Write a python program to print from line 2 to line . (assuming the file has more
than 5 lines)

50)Write a python program to read last 2 lines of a text file.

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.

55)Write function definition for WORD4CHAR() in Python to read the content of a


text file FUN.TXT and display all those words, which has four characters in it.

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.

58)Program in python to append text at the end of text file

f=open("d:\\a.txt","a")

s=input("enter a string to add at the end of file")

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()

print(‘Number of lines starting with A in the file LINES.txt is’, func(l))

*****

You might also like