Bird
0
0

Given a list of filenames files = ['a.txt', 'b.txt', 'c.txt'] and a folder path folder = '/data', which code creates a list of full paths correctly?

hard📝 Application Q9 of 15
Python - Standard Library Usage
Given a list of filenames files = ['a.txt', 'b.txt', 'c.txt'] and a folder path folder = '/data', which code creates a list of full paths correctly?
A[os.path.join(folder + f) for f in files]
B[folder + '/' + f for f in files]
C[os.path.join(f, folder) for f in files]
D[os.path.join(folder, f) for f in files]
Step-by-Step Solution
Solution:
  1. Step 1: Use list comprehension with os.path.join

    Join folder and each filename as separate arguments inside comprehension.
  2. Step 2: Avoid string concatenation or wrong argument order

    Concatenation may cause wrong separators; order matters for correct paths.
  3. Final Answer:

    [os.path.join(folder, f) for f in files] -> Option D
  4. Quick Check:

    List comprehension with join(folder, file) [OK]
Quick Trick: Use join(folder, filename) inside list comprehension [OK]
Common Mistakes:
  • Concatenating strings manually
  • Swapping join arguments
  • Passing single string to join

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes