Bird
Raised Fist0

Why does os.path.join('folder', '/file.txt') ignore the first argument and return '/file.txt'?

hard🧠 Conceptual Q10 of Q15
Python - Standard Library Usage
Why does os.path.join('folder', '/file.txt') ignore the first argument and return '/file.txt'?
ABecause '/file.txt' is an absolute path, resetting the join
BBecause os.path.join only joins relative paths
CBecause the first argument must be absolute
DBecause join concatenates strings without separator
Step-by-Step Solution
Solution:
  1. Step 1: Understand absolute path behavior in os.path.join

    If any argument is absolute, all previous parts are discarded and join starts from that argument.
  2. Step 2: Apply to given example

    Since '/file.txt' starts with '/', it is absolute, so 'folder' is ignored.
  3. Final Answer:

    Because '/file.txt' is an absolute path, resetting the join -> Option A
  4. Quick Check:

    Absolute path resets join parts [OK]
Quick Trick: Absolute path in join resets previous parts [OK]
Common Mistakes:
MISTAKES
  • Thinking join always concatenates
  • Ignoring absolute path rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes