Which of the following is the correct syntax to create a Path object for a file named 'data.txt' in folder 'docs'?
easy📝 Syntax Q3 of Q15
Python - File Handling Fundamentals
Which of the following is the correct syntax to create a Path object for a file named 'data.txt' in folder 'docs'?
APath('docs/data.txt')
BPath('docs') / 'data.txt'
CPath('docs', 'data.txt')
DAll of the above
Step-by-Step Solution
Solution:
Step 1: Understand Path object creation
Path can be created by joining strings or using the division operator '/' to join paths.
Step 2: Check each option
Path('docs', 'data.txt') uses multiple arguments, valid in pathlib. Path('docs/data.txt') uses a single string with slash. Path('docs') / 'data.txt' uses '/' operator to join paths. All are valid.
Final Answer:
All of the above -> Option D
Quick Check:
Multiple ways to create Path = all valid [OK]
Quick Trick:Use '/' operator or multiple args to join paths [OK]
Common Mistakes:
MISTAKES
Thinking only '/' operator works
Assuming multiple args not allowed
Confusing string concatenation with Path
Master "File Handling Fundamentals" in Python
9 interactive learning modes - each teaches the same concept differently