Python Programming - Reading and Writing Files
Exercise : Reading and Writing Files - General Questions
- Reading and Writing Files - General Questions
31.
How can you read and print the first 10 characters from a file named "sample.txt"?
Answer: Option
Explanation:
Using
read(10)
to read and print the first 10 characters of the file.
32.
What is the purpose of the
os.rename()
function?
Answer: Option
Explanation:
os.rename()
is used to rename a file or directory in Python.
33.
How can you read and print only the lines that start with "Error:" from a log file named "logfile.txt"?
Answer: Option
Explanation:
Using
startswith()
to filter and print lines starting with "Error:".
34.
What is the purpose of the
shutil.move()
function?
Answer: Option
Explanation:
shutil.move()
is used to move a file or directory to a different location.
35.
How can you determine the number of lines in a file named "data.txt"?
Answer: Option
Explanation:
Using
readlines()
and len()
to count the number of lines in the file.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers