Python Programming - Reading and Writing Files
Exercise : Reading and Writing Files - General Questions
- Reading and Writing Files - General Questions
26.
How can you append the string "new content" to an existing file named "existing_file.txt"?
Answer: Option
Explanation:
The mode "a" in
open()
is used for appending content to an existing file.
27.
What does the
os.path.getsize()
function return?
Answer: Option
Explanation:
os.path.getsize()
returns the size of a file in bytes.
28.
How can you read and print the last 5 lines from a file named "log.txt"?
Answer: Option
Explanation:
Using
readlines()
and list slicing to get the last 5 lines.
29.
What is the purpose of the
os.path.abspath()
function?
Answer: Option
Explanation:
os.path.abspath()
returns the absolute path of a file.
30.
How can you check if a file named "data.csv" is empty?
Answer: Option
Explanation:
Using
os.path.getsize()
to check if the file size is 0, indicating an empty file.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers