Bird
Raised Fist0

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:
  1. Step 1: Understand Path object creation

    Path can be created by joining strings or using the division operator '/' to join paths.
  2. 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.
  3. Final Answer:

    All of the above -> Option D
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes