Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q13 of Q15
Python - Standard Library Usage
What will be the output of this code?
import os
path = os.path.join('folder', 'subfolder', 'file.txt')
print(os.path.basename(path))
Afolder
Bsubfolder
Cfile.txt
Dfolder/subfolder/file.txt
Step-by-Step Solution
Solution:
  1. Step 1: Understand os.path.join()

    It creates 'folder/subfolder/file.txt' (with correct separator).
  2. Step 2: Understand os.path.basename()

    It returns the last part of the path, which is the file name 'file.txt'.
  3. Final Answer:

    file.txt -> Option C
  4. Quick Check:

    basename() returns file name [OK]
Quick Trick: basename() returns last part (file) of path [OK]
Common Mistakes:
MISTAKES
  • Expecting folder name instead of file
  • Confusing join() output with basename()
  • Printing full path instead of basename

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes