100% found this document useful (1 vote)
727 views7 pages

Assignment Text File Handling

The document discusses various methods for reading, writing, and manipulating text files in Python. It provides examples of opening text files using different modes like 'r', 'w', and 'r+' and using methods like read(), readline(), readlines() to read file contents. It also discusses using seek() to move the file pointer and tell() to get the byte position of the file pointer. The questions aim to test understanding of correct usage of opening, reading, writing and other file handling methods in Python.

Uploaded by

Ashish Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
727 views7 pages

Assignment Text File Handling

The document discusses various methods for reading, writing, and manipulating text files in Python. It provides examples of opening text files using different modes like 'r', 'w', and 'r+' and using methods like read(), readline(), readlines() to read file contents. It also discusses using seek() to move the file pointer and tell() to get the byte position of the file pointer. The questions aim to test understanding of correct usage of opening, reading, writing and other file handling methods in Python.

Uploaded by

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

FILE HANDLING – TEXT FILES

1 Which of the following statement is not correct ? 1 2022


(a) We can write content into a text file opened using 'w' mode term 1
(b) We can write content into a text file opened using 'w+' mode
(c) We can write content into a text file opened using 'r' mode
(d) We can write content into a text file opened using 'r+' mode
2 Which of the following option is correct Python statement to read and display the first 10 1 2022
characters of a text file “Notes.txt”. term 1
(a) F = open('Notes.txt')
print(F.load(10))
(b) F = open('Notes.txt')
print(F.dump(10))
(c) F = open('Notes.txt')
print(F.read(10))
(d) F = open('Notes.txt')
print(F.write(10))
3 Which of the following is not a correct Python statement to open a text file Notes. txt" to write 1 2022
content into it ? term 1
(a) F = open('Notes.txt', 'w')
(b) F = open('Notes.txt', 'a')
(c) F = open('Notes.txt', 'A')
(d) F = open('Notes.txt', 'w+')
4 A text file opened using the following statement : 1 2022
MyFile = open('Notes.txt') term 1
Which of the following is the correct Python statement to close it ?
(a) MyFile=close('Notes.txt')
(b) MyFile.close('Notes.txt')
(c) close.MyFile()
(d) MyFile.close()
5 Which of the following option is the correct usage for the tell( ) of a file 1 2022
(a) It places the file pointer at a desired offset in a file term 1
(b) It returns the entire content of a file
(c) It returns the byte position of the file pointer as an integer
(d) It tells the details about the file
6 What is the significance of the seek( ) method ? 1 2022
(a) It seeks the absolute path of the file. term 1
(b) It tells the current byte position of the file pointer within the file.
(c) It places the file pointer at a desired offset within the file.
(d) It seeks the entire content of the file.
7 If the following statement is used to read the contents of a textfile object F: 1 2022
X=F.readlines() term 1
Which of the following is the correct data type of x ?
(a) string (b) list (c) tuple (d) dictionary
8 Suppose the content of a text file Notes.txt is : "The way to get started is to 1 2022
quit talking and begin doing" term 1
What will be the output of the following Python code ?
F = open("Notes.txt")
F.seek(29)
S = F.read() print(S)
(a) The way to get started is to
(b) quit talking and begin doing
(c) The way to get started is to quit talking and begin doing
(d) gniod nigeb dna gniklat tiuq ot si detrats teg ot yaw ehT
9 Suppose the content of a text file "Rhymes.txt" is as follows : 1 2022
Jack & Jill term 1
Went uo the hill
What will be the output of the following Python code ?
F = open("Rhymes.txt")
L = F.readlines()
for i in L:
S=i.split()
print(len(S),end="#")
(a) 2#4# (b) 3#4# (c) 2# (d) 7#
10 Suppose the content of "Rhymes.txt" is : 1 2022
Baa baa black sheep, term 1
have you any wool?
What will be the output of the following Python code ?
F = open("Rhymes.txt")
S = F.read()
L=S.split ()
for i in L :
if len(i) %3 != 0 :
print(i, end= " ")
(a) Baa baa you any (b) black have wool? (c) black sheep, have wool? (d) Error
11 Suppose the content of "Rhymes.txt" is 1 2022
One, two, three, four, five term 1
Once I caught a fish alive.
What will be the output of the following Python code ?
F = open("Rhymes.txt")
S = F.read()
print(S.count('e',20))
(a) 20 (b) 1 (c) 3 (d) 6
12 Suppose the content of "Rhymes.txt" is 1 2022
Good Morning Madam term 1
What will be the output of the following Python code ?
F = open("Rhymes.txt")
L = F.read().split()
for W in L:
if W.lower() == W[::-1].lower():
print(W)
(a) Good (b) Morning (c) Madam (d) Error
13 Suppose the content of "Rhymes.txt" is 1 2022
Hickory Dickory Dock term 1
The mouse went up the clock
What will be the output of the following Python code ?
F = open("Rhymes.txt")
L = F.readlines()
X = ["the", "ock"]
for i in L:
for W in i.split() :
if W in X:
print(W,end = "*")
(a) the* (b) Dock*The*the*clock*
(c) Dock*the*clock* (d) Error
14 Consider the following directory structure. 1 2022
term 1

Suppose the present working directory is MyCompany. What wil l be the relative path of the file
Transactions. Dat?
(a) MyCompany/Transactions.Dat (b) MyCompany/Accounts/Transactions.Dat
(c) Accounts/Transactions.Dat (d) ../Transactions.Dat
15 Which of the following statements is not correct? 1 SQP
a. If we try to read a text file that does not exist, an error occurs. 2022
b. If we try to read a text file that does not exist, the file gets created. Term 1
c. If we try to write on a text file that does not exist, no error occurs.
d. If we try to write on a text file that does not exist, the file gets created.
16 Which of the following options can be used to read the first line of a text file "Myfile.txt"? 1 SQP
a. myfile = open('Myfile.txt') 2022
print(myfile.read(line)) Term 1
b. myfile = open('Myfile.txt','r')
print(myfile.read(1))
c. myfile = open('Myfile.txt')
print(myfile.readline())
d. myfile = open('Myfile.txt')
print(myfile.readlines())
17 Assume that the position of the file pointer is at the beginning of 3rd line in a text file. Which of 1 SQP
the following options can be used to read all the remaining lines? 2022
a. myfile.read(n-3) b. myfile.read(n) Term 1
c. myfile.readline() d. myfile.readlines()
18 A text file "student.txt" is stored in the storage device. Identify the correct option out of the 1 SQP
following options to open the file in read mode. 2022
i. myfile = open('student.txt','a') Term 1
ii. myfile = open('student.txt','w')
iii. myfile = open('student.txt','r')
iv. myfile = open('student.txt')
a. only I b. both i and iv c. both iii and iv d. both i and iii
19 What is the significance of the tell() method? 1 SQP
a. tells the path of the file. 2022
b. tells the current position of the file pointer within the file. Term 1
c. tells the end position within the file.
d. checks the existence of a file at the desired location.
20 Syntax of seek function in Python is myfile.seek(offset, reference_point) where SQP
myfile is the file object. What is the default value of reference_point? 2022
a. 0 b. 1 c. 2 d. 3 Term 1
21 Suppose the content of Myfile.txt is: 1 SQP
Twinkle twinkle little star 2022
How I wonder what you are Term 1
Up above the world so high
Like a diamond in the sky
What will be the output of the following code?
myfile = open("Myfile.txt")
data = myfile.readlines()
print(len(data))
myfile.close()
a. 3 b. 4 c. 106 d. 22
22 Suppose the content of "Myfile.txt" is 1 SQP
Humpty Dumpty sat on a wall 2022
Humpty Dumpty had a great fall Term 1
All the king's horses and all the king's men
Couldn't put Humpty together again
What will be the output of the following code?
myfile = open("Myfile.txt")
record = myfile.read().split()
print(len(record))
myfile.close()
a. 24 b. 25 c. 26 d. 27
23 Suppose the content of "Myfile.txt" is 1 SQP
Honesty is the best policy. 2022
What will be the output of the following code? Term 1
myfile = open("Myfile.txt")
x = myfile.read()
print(len(x))
myfile.close()
a. 5 b. 25 c. 26 d. 27
24 Suppose the content of "Myfile.txt" is 1 SQP
Humpty DUMPTY sat on a wall 2022
Humpty DUMPTY had a great fall Term 1
All the king's horses and all the king's men
Couldn't put Humpty together again
What will be the output of the following code?
myfile = open("Myfile.txt")
x = myfile.read().lower()
y = x.count('umpty')
print(y)
myfile.close()
a. 2 b. 3 c. 4 d. 5
25 Suppose the content of "Myfile.txt" is 1 SQP
Ek Bharat Shreshtha Bharat 2022
What will be the output of the following code? Term 1
myfile = open("Myfile.txt")
vlist = list("aeiouAEIOU")
vc=0
x = myfile.read()
for y in x:
if(y in vlist):
vc+=1
print(vc)
myfile.close()
a. 6 b. 7 c. 8 d. 9
26 Suppose the content of "Myfile.txt" is 1 SQP
Twinkle twinkle little star 2022
How I wonder what you are Term 1
Up above the world so high
Like a diamond in the sky
Twinkle twinkle little star
What will be the output of the following code?
myfile = open("Myfile.txt")
line_count = 0
data = myfile.readlines()
for line in data:
if line[0] == 'T':
line_count += 1
print(line_count)
myfile.close()
a. 2 b. 3 c. 4 d. 5
27 Consider the following directory structure. 1 SQP
2022
Term 1
Suppose root directory (School:\) and present working directory are the same. What will be
the absolute path of the file Syllabus.jpg?
a. School/Syllabus.jpg
b. School/Academics/Syllabus.jpg
c. School/Academics/../Syllabus.jpg
d. School/Examination/Syllabus.jpg
28 Assume the content of the text file "Myfile.txt" is: 1 SQP
Arjun Kumar 2022
Ismail Khan Term 1
Joseph B
Hanika Kiran
What will be the data type of data_rec?
myfile = open("Myfile.txt")
data_rec = myfile.readlines()
myfile.close()
a. string b. list c. tuple d. dictionary
29 Write a function in Python that counts the number of “Me” or “My” words present in a text file 3 SQP
“STORY.TXT”. 2022
Term 1
If the “STORY.TXT” contents are as follows:
My first book was Me and
My Family. It gave me chance to be Known to the world.
The output of the function should be:
Count of Me/My in file: 4
30 Write a function AMCount() in Python, which should read each character of a text file 3 SQP
STORY.TXT, should count and display the occurrence of alphabets A and M (including small 2022
Term 1
cases a and m too).
Example: If the file content is as follows:
Updated information
As simplified by official websites.
The AMCount() function should display the output as:
A or a:4
M or m :2
31 Write a function in python to count the number of lines in a text file ‘STORY.TXT’which is 3 SQP
starting with an alphabet ‘A’ . 2019
32 Write a method/function DISPLAYWORDS() in python to read lines from a text file STORY.TXT, 3 SQP
and display those words, which are less than 4 characters. 2019
33 Differentiate between ‘‘w’’ and ‘‘r’’ file modes used in Python. Illustrate the difference using 2 Comptt
suitable examples. 2020 -
New
34 Write a function Show_words() in Python to read the content of a text file 'NOTES.TXT' and 2 Comptt
display the entire content in capital letters. Example, if the file contains: 2020 -
"This is a test file" New
Then the function should display the output as :
THIS IS A TEST FILE
35 Write a function Show_words() in Python to read the content of a text file 'NOTES.TXT' and 2 Comptt
display only such lines of the file which have exactly 5 words in them. Example, if the file 2020 -
contains: New
This is a sample file.
The file contains many sentences.
But needs only sentences which have only 5 words.
Then the function should display the output as :
This is a sample file.
The file contains many sentences.
36 Write a statement in Python to open a text file CONTENT.TXT so that new contents can be 1 Comptt
written in it. 2019 -
Old
37 Write a statement in Python to open a text file REMARKS.TXT so that existing content can be 1 Comptt
read from it. 2019 -
Old
38 Write a method/function BIGWORDS() in Python to read contents from a text file CODE.TXT, 2 Comptt
to count and display the occurrence of those words, which are having 5 or more alphabets. 2019 -
For example : Old
If the content of the file is
ME AND MY FRIENDS
ENSURE SAFETY AND SECURITY OF EVERYONE
The method/function should display
Count of big words is 5
39 Write a method/function BIGLINES() in Python to read lines from a text file CONTENT.TXT, 2 Comptt
and display those lines, which are bigger than 20 characters. 2019 -
For example : Old
If the content of the file is
Stay positive and happy
Work hard and dont give up hope
Be open to criticism and keep learning
Surround yourself with happy, warm and genuine people
The method/function should display
Be open to criticism and keep learning
Surround yourself with happy, warm and genuine people
40 Write a statement in Python to open a text file WRITEUP.TXT so that new content can be written 1 2019 –
in it. Old
41 Write a statement in Python to open a text file README.TXT so that existing content can be read 1 2019 –
from it. Old
42 Write a method/function ISTOUPCOUNT() in python to read contents from a text file 2 2019 –
WRITER.TXT, to count and display the occurrence of the word ‘‘IS’’ or ‘‘TO’’ or ‘‘UP’’. Old
For example :
If the content of the file is
IT IS UP TO US TO TAKE CARE OF OUR SURROUNDING. IT
IS NOT POSSIBLE ONLY FOR THE GOVERNMENT TO TAKE
RESPONSIBILITY
The method/function should display
Count of IS TO and UP is 6
43 Write a method/function AEDISP() in python to read lines from a text file WRITER.TXT, and 2 2019 –
display those lines, which are starting either with A or starting with E. Old
For example :
If the content of the file is
A CLEAN ENVIRONMENT IS NECESSARY FOR OUR GOOD HEALTH.
WE SHOULD TAKE CARE OF OUR ENVIRONMENT.
EDUCATIONAL INSTITUTIONS SHOULD TAKE THE LEAD.
The method should display
A CLEAN ENVIRONMENT IS NECESSARY FOR OUR GOOD HEALTH.
EDUCATIONAL INSTITUTIONS SHOULD TAKE THE LEAD.
A text file named SOLUTION.TXT contains some English sentences. Another text file named 3 Comptt
TEST.TXT needs to be created such that it replaces every occurence of 3 consecutive letters ‘h’, 2020 -
‘i’ and ‘s’ (irrespective of their cases) from each word of the file SOLUTION.TXT, with 3 Old
underscores (‘___’).
For example : If the file SOLUTION.TXT contains the following content :
"This is his history book."
Then TEST.TXT should contain the following :
"T ___ is ___ ___tory book."
Write the definition for function CreateTest()in Python that would perform the above task of
creating TEST.TXT from the already existing file SOLUTION.TXT.
A text file named AGENCIES.TXT contains some text. Write the definition for a function 3 Comptt
Showsites() in Python which displays all such words of the file which have more than 9 characters 2020 -
and start with "www.". Old
For example : If the file AGENCIES.TXT contains :
"Name: TechnoCraft, Website: www.technocraft.com,
Name: DataTech, Website: www.datatech.com" 3
Then the function Showsites() should display the output as :
www.technocraft.com
www.datatech.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy