MCQ-Python FILE HANDLING-QB
MCQ-Python FILE HANDLING-QB
(b) fobj.read()
1. To open a file c:\test.txt for reading, we should (c) fobj.readline()
give the statement: (d) fobj.readlines()
f = None
3. To open a file c:\ test.txt for appending data, we for i in range (5):
can give the statement: with open ("data.txt", "W") as f:
(a) fobj.read(2)
S SHUNMUGA SUNDARAM , M.E/CS, A.M.I.E, MISTE, IAENG..
(a) fobj.read(2) (c) fobj.writelines(sequence)
(b) fobj.read() (d) fobj.writeline()
(c) fobj.readline()
(d) fobj.readlines
15. In file handling, what do the terms "r" and "a"
stand for?
10. The readlines() method returns:
(a) read, append
(a) String (b) append, read
(b) A list of integers (iii) write, append
(c) A list of single characters (d) None of the above
(d) A list of lines
(b) fobj.writelines()
S SHUNMUGA SUNDARAM , M.E/CS, A.M.I.E, MISTE, IAENG..
(a) Cursor Separated Variables (c) +
(b) Comma Separated Values (d) b
(c) Cursor Separated Values
(d) Cursor Separated Version
25. Every record in a CSV file is stored in reader
object in the form of a list using which method?
20. Which module is used for working with CSV
files in Python? (a) writer()
(b) append()
(a) random (c) reader()
(b) statistics (d) list()
(c) csv
(d) math
26. Information stored on a storage device with a
specific name is called a _____.
21. Which of the following modes is used for both
writing and reading from a binary file? (a) array
(b) dictionary
(a) wb+ (c) file
(b) w (d) tuple
(c) wb
(d) w+
27. Which of the following format of files can be
created programmatically through Python to some
22. Which statement is used to retrieve the data?
current position within the file?
(a) Data files
(a) fp.seek() (b) Text files
(b) fp.tell() (c) Video files
(c) fp.loc (d) Binary files
(d) fp.pos
(b) w
S SHUNMUGA SUNDARAM , M.E/CS, A.M.I.E, MISTE, IAENG..
(a) infi.read(all) (c) readall()
(b) infi.read() (d) readchar()
(c) infi.readline()
(d) infi.readlines()
35. Which function is used to read single line from
file?
30. To read the remaining lines of the file from a
file object infi, we use (a) readline()
(b) readlines()
(a) infi.read(all) (c) readstatement( )
(b) infi.read() (d) readfulline()
(c) infi.readline()
(d) infi.readlines()
36. Which function is used to write all the
characters?
31. The readlines() method returns
(a) write()
(a) str (b) writecharacters()
(b) a list of lines (c) writeall()
(c) a list of single characters (d) writechar()
(d) a list of integers
Answers:
26. c
Page
file.
27. b, d
S SHUNMUGA SUNDARAM , M.E/CS, A.M.I.E, MISTE, IAENG..
9. A _____ file is a series of 1's and 0's, treated as 22. _____ object contains the number of the
raw data and read byte-by-byte. current line in a CSV file.
10. The _____ statement automatically closes the 23. To end all the file contents in the form of a list,
file after the processing on the file gets over. _____ method may be used.
11. The read() function reads data from the _____ 24. To read all the file contents, _____ method is
of a file. used.
12. The pickle module produces two main methods 25. To force Python to write the contents of file
called _____ and ____ for writing and reading buffer on to storage file, _____ method may be
operations. used.
13. The readlines() returns a list of lines from the 26. The default file-open mode is _____ mode.
file till _____.
27. A _____ governs the type of operations (e.g.,
14. The _____ method reads 'n' characters from read/write/append) possible in the opened file.
the file.
28. The two types of data files can be _____ files
15. _____ function is used to force transfer of data and _____ files.
from buffer to file.
29. The other name for file object is _____.
16. _____ format is a text format accessible to all
applications across several platforms. 30. The _____ file mode will open a file for read
and write purpose.
17. _____ method is used for random access of
data in a CSV file. 31. The _____ file mode will open a file for write
and read purpose.
18. _____ method of pickle module is used to
write an object into binary file. 32. To close an open file, _____ method is used.
19. _____ method of pickle module is used to read 33. To read all the file contents in form of a list,
data from a binary file. ______ method is used.
20. _____ statement is given for importing csv 34. To write a list in a file, _____ method may be
module into your program. used.
6
21. _____ is a string method that joins all values of 35. To force Python to write the contents of file
Page
each row with comma separator in CSV. buffer on to storage file, _____ method may be
S SHUNMUGA SUNDARAM , M.E/CS, A.M.I.E, MISTE, IAENG..
used. File Handling || True or False || Class 12
Answers:
1. Files
1. An absolute path always begins with the root
2. open()
3. read ('r'), write ('w'), append ('a') folder.
4. writelines()
5. close() 2. It is not necessary to always create the file in the
6. getcwd() same default folder where Python has been
7. rename() installed.
8. remove()
9. binary
3. In binary file, there is no delimiter for a line.
10. with
11. (beginning)
12. dump(), load() 4. A binary file stores information in ASCII or
13. end of file (EOF) Unicode characters.
14. read(n)
15. flush() 5. readline() reads the entire file at a time.
16. CSV
17. seek()
6. A close() function breaks the link of the file
18. dump()
19. load() object and the file on the disk.
20. import csv
21. join() 7. When you open a file in read mode, the given
22. line_num file must exist in the folder, otherwise Python will
23. readlines() raise FileNotFound error.
24. writelines()
25. flush()
8. The default file open mode is write mode.
26. read
27. file mode
28. text, binary 9. Opening a file in append mode erases the
29. file handle previous data.
30. r+
31. w+ or a+ 10. A file mode defines the type of operations that
32. close()
is to be performed on the file.
33. readlines()
7
34. writelines()
Page
12. readlines() function returns a list of strings, 23. tell() method of Python tells us the current
each separated by "\n". position within the file.
13. file() and open() functions have different 24. The offset argument to seek() method
usage. indicates the number of bytes to be moved.
14. Data maintained inside the file is termed as 25. If the offset value is set to 2, beginning of the
"persistent” (permanent in nature) data. file would be taken as seek position.
15. with statement ensures that all the resources 26. When you open a file for reading, if the file
allocated to the file objects get deallocated does not exist, an error occurs.
automatically.
27. When you open a file for writing, if the file
16. CSV module can handle CSV files correctly does not exist, an error occurs.
regardless of the operating system on which the
files were created. 28. When you open a file for writing, if the file
exists, the existing file is overwritten with the new
17. CSV module gets automatically imported into file.
your program for reading a CSV file.
29. The absolute paths are from the topmost level
18. The type of operation that can be performed of the directory structure.
on a file depends upon the file mode in which it is
opened. 30. The relative paths are relative to current
working directory.
19. Functions readline() and readlines() are
essentially the same. 31. The relative path for a file always remains
same even after changing the directory.
20. Every record in a CSV file is stored in reader
object in the form of a list. 32. The types of operations that can be carried out
on a file depends upon the file mode a file is
21. writerow() method allows us to write a list of opened in.
fields to the CSV file.
8
Page
21. True
9
Page
22. True
S SHUNMUGA SUNDARAM , M.E/CS, A.M.I.E, MISTE, IAENG..
Q. Differentiate between file modes r+ and rb+
with respect to Python.
Q. Write a function countmy() in Python to read (a) What would be the output of following code?
the text file "DATA.TXT" and count the number of
times "my" occurs in the file. file = open ("sarojiniPoem.txt","r")
For example, if the file "DATA.TXT" contains "This text = file.readlines()
is my website. I have displayed my preferences in file.close()
the CHOICE section." - the countmy() function for line in text :
should display the output as: "my occurs 2 times". print (line , end = ' ' )
print ( )
Q. Write a program using Dictionary and Text files Obtain the details from the user.
to store roman numbers and find their
equivalents.
(a) To process four files sequentially. Q. Write a function in python to count the number
(b) To merge two sorted files into third file. of lines in a text file 'STORY.TXT' which is starting
with an alphabet 'A'.
value)
Q. Write code to print just the last line of a text file
Page
Write a function in Python to update the file with a with respect to python.
new value of stock. The stock and the
product_code, whose stock is to be updated, are
to be inputted during the execution of the
function.
Q. Differentiate between file modes r+ and rb+
with respect to Python.
to the second file. file after removing EOL characters, leading and
Page
Autumn Song
Q. Consider a binary file Employee.dat containing
details such as empno: ename: salary (separator Like a joy on the heart of a sorrow,
':'). Write a Python function to display details of The sunset hangs on a cloud;
those employees who are earning between 20000 A golden storm of glittering sheaves,
and 40000 (both values inclusive). Of fair and frail and fluttering leaves,
The wild wind blows in a cloud.
question:
Page
(b) Modify the program so that the lines are Q. How many file objects would you need to
printed in reverse order. manage the following situations:
(c) Modify the code so as to output to another file (a) To process four files sequentially.
instead of the screen. Let your script overwrite the (b) To merge two sorted files into third file.
output file.
Kriti 1010101
Page
Q. Write a function in Python to count the number Q. Create a CSV file "Groceries" to store
of lines in a text file 'STORY.TXT' which are starting information of different items existing in a shop.
with the alphabet 'A'. The information is to be stored w.r.t. each item
code, name, price, qty. Write a program to accept
the data from user and store it permanently in CSV
file.
Q. A text file contains alphanumeric text (say
an.txt). Write a program that reads this text file
and prints only the numbers or digits from the file.
value)
Page