Python Programming - Reading and Writing Files
Exercise : Reading and Writing Files - General Questions
- Reading and Writing Files - General Questions
41.
What is the purpose of the
os.path.basename()
function?
Answer: Option
Explanation:
os.path.basename()
returns the base name of a file or directory.
42.
How can you read and print only the lines longer than 20 characters from a file named "data.txt"?
Answer: Option
Explanation:
Using
len()
to filter and print lines longer than 20 characters.
43.
What does the
shutil.rmtree()
function do?
Answer: Option
Explanation:
shutil.rmtree()
removes a directory and its contents recursively.
44.
How can you copy a file named "source.txt" to a new location and rename it as "destination.txt"?
Answer: Option
Explanation:
Using
shutil.copy()
to copy and rename a file.
45.
How can you check if a file named "config.ini" exists?
Answer: Option
Explanation:
Using
exists()
to check if a file exists.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers