0% found this document useful (0 votes)
17 views2 pages

cs12 - TEXT FILEs

Uploaded by

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

cs12 - TEXT FILEs

Uploaded by

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

VAILANKANNI PUBLIC SCHOOL – CBSE,

( SENIOR SECONDARY )
BARGUR.
WORK SHEET – 3 ( Text Files)
XII – COMPUTER SCIENCE
1. observe the following code and answer the follow iii) Complete the code for statement 3 by replacing
f1=open("mydata","a") the spaces with the code for writing multiple
______#blank1 strings/lines at once in the file.
f1.close()
(A) writeline() (B) writelines(l)
Fill in the blank1 with correct statement to write "abc" (C) writelines() (D) writestrings()
in the file "mydata"
(A) f1.write(“abc”) (B) write(abc) iv) Replace the code for statement 4 by closing the
(C) write.f1(abc) (D) None of the above file.
(A) f.close() (B) close()
2. If the content of the file “wish.txt” is – “Happy”, (C) close(f) (D) fout.close()
then what will be the content of the file after
executing the following statements –
f=open (“wish.txt”, ‘w’) 4. Deepti wants to write into an existing file ‘story.txt’
f. write(“Birthday”) which already has some important content in it. She
f. close() has written the code given below. Help her to
complete this code and analyze the output.
(A) Happy Birthday (B) HappyBirthday
(C) Happy (D) Birthday fout = ________________________ # STATEMENT 1
print("Name of the file:", fout.name)
3. Ramesh wants to write into an existing file
n = int(input("No. of lines to be entered:"))
‘story.txt’ which already had some important content
for i in range(n):
in it. He has APPLICATION, written the code given
fil_str = input("Enter string:")
below. Help him to complete this code.
fil_str = fil_str+"\n"
fout = open('story.txt',_________) # STATEMENT 1
fout.______________ # STATEMENT 2
print("Name of the file:", fout.name)
________________ # STATEMENT 3
n = int(input("No. of lines to be entered:"))
l = []
i) Complete the code for statement 1 by writing code
for i in range(n):
to open the file for writing. (Consider file already
fil_str = input("Enter string:")
exists with some important data)
fil_str = _________________ # STATEMENT 2
(A) open() (B) open(story.txt,’w’)
l.append(fil_str)
(C) open(‘story.txt’,’a’) (D) open(‘story.txt’)
fout._______________ # STATEMENT 3
__________________ # STATEMENT 4
ii) Complete the code for statement 2 by replacing the
spaces with the code for writing single string/line at a
i) Complete the code for statement 1 by writing
time in the file.
access mode to open the file for writing.
(A) writeline() (B) writeline(fil_str)
(Consider file already exists with some important
(C) writes(fil_str) (D) write(fil_str)
data)
(A) a (B) r (C) w (D)
iii) Replace the code for statement 3 by closing the
file.
ii) Complete the code for statement 2 by adding a
(A) f.close() (B) close()
newline character to the string entered by the user.
(C) close(f) (D) fout.close()
(A) ‘\n’ (B) fil_str+newline
(C) fil_str+’\n’ (D) fil_str+’\N’
iv) If user provides two strings as input : 5 Fill in the blank in Statement-5 to close the file.
String 1 : “PYTHON IS AN EASY LANGUAGE” a) file.close() b) File.Close()
String 2 : “PYTHON IS POPULAR LANGUAGE” c) Close() d) end()
What will be the content of the file after execution of
the code?
(A) PYTHON IS AN EASY LANGUAGE PYTHON IS
POPULAR LANGUAGE 6. Suppose the content of "Myfile.txt" is
(B) PYTHON IS AN EASY LANGUAGE PYTHON IS Humpty Dumpty sat on a wall
POPULAR LANGUAGE Humpty Dumpty had a great fall
(C) PYTHON IS AN EASY LANGUAGEPYTHON IS All the king's horses and all the king's men
POPULAR LANGUAGE Couldn't put Humpty together again
(D) PYTHON IS POPULAR LANGUAGEPYTHON IS AN Ravi, a student of class 12, is learning Python. During
EASY LANGUAGE the examination he has been given a stub Python
program (shown below) to count no of independent
5. Your teacher has given you a method/function words has started from vowels. Help him in
FilterWords() in python which reads data from a text completing the code.
file NewsLetter.TXT. Your teachers intentionally kept
few blanks in between the code and asked you to fill myfile = open("Myfile.txt", _____) # Statement 1
the blanks so that the code will run to find desired vlist = list("aeiouAEIOU")
result. Do the needful with the following python code. vc=0
s = myfile.read()
def FilterWords(): w=_________________ # Statement 2
c=0 for y in _______: # Statement 3
file=open('NewsLetter.TXT', '_') #Statement-1 if ____ in _____: #Statement 4
data = file._____ #Statement-2 vc+=1
line = _____ #Statement-3 print(vc)
linecount= _____: #Statement-4 myfile._________ # statement 5
print(“No of lines”,linecount)__ #Statement-5
(i) Identify the suitable code for blank space in the line
FilterWords() marked as Statement-1.
A. ‘w’ B. ‘w+’ C. ‘rb’ D. ‘r+’
1 Fill in the statement 1 with appropriate access mode
a) rb b) r c) w d) a (ii) Identify the suitable code for blank space in the
line marked as Statement-2.
2 Fill the statement 2 with appropriate function to A. Vlist.split() B. S.split(‘*’)
read 5 characters C. s.split() D. S.split()
a) read() b) read(5)
c) readline(5) d) get(5) (iii) Identify the suitable code for blank space in the
line marked as Statement-3.
3 Fill the statement 3 to read the remaining content of A. vlist B. s C. w D. myfile
the file in list form.
a) file.read() b) file.readlines() (iv) Identify the suitable code for blank space in the
c) file.readline() d) readlines() line marked as Statement-4.
A. y & w B. y[0] & s C. y & s D. y[0] & w
4 Fill the statement 4 to count the no. of lines in the
file. (v) Identify the suitable code for blank space in the
a) len() b) len(line) c) Len(line) d) len.line line marked as Statement-5.
A. Off() B. Close() C. close() D. open()

You might also like