Python Programming - Reading and Writing Files
Exercise : Reading and Writing Files - General Questions
- Reading and Writing Files - General Questions
1.
How can you open a file named "example.txt" in Python for reading?
Answer: Option
Explanation:
To open a file for reading, you should use the mode "r" in the
open()
function.
2.
How can you read the entire contents of a file named "data.txt" into a string?
Answer: Option
Explanation:
Using the
read()
method on the file object reads the entire contents of the file into a string.
3.
What is the purpose of the
write()
method when used with a file object?
Answer: Option
Explanation:
The
write()
method is used to write data to a file in Python.
4.
How can you close a file in Python after performing operations on it?
Answer: Option
Explanation:
To close a file in Python, you should use the
close()
method on the file object.
5.
In Python, what does the
with
statement provide when working with files?
Answer: Option
Explanation:
The
with
statement is used for efficient file handling and automatically closes the file when the block is exited.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers