Friday , September 20 2024

What is the expected result of the following code? import os os.mkdir(‘thumbnails’) os.chdir(‘thumbnails’) sizes = [‘small’, ‘medium’, ‘large’] for size in sizes: os.mkdir(size) print(os.listdir())

Questions BankCategory: Python Essentials 2What is the expected result of the following code? import os os.mkdir(‘thumbnails’) os.chdir(‘thumbnails’) sizes = [‘small’, ‘medium’, ‘large’] for size in sizes: os.mkdir(size) print(os.listdir())
What is the expected result of the following code?

import os
 
os.mkdir('thumbnails')
os.chdir('thumbnails')
 
sizes = ['small', 'medium', 'large']
 
for size in sizes:
    os.mkdir(size)
 
print(os.listdir())
  • [‘.’, ‘large’, ‘small’, ‘medium’]
  • [‘large’, ‘small’, ‘medium’]
  • [‘.’, ‘..’, ‘large’, ‘small’, ‘medium’]
  • []

Explanation: Let’s analyze this code snippet:

  • The first line imports the os module, which handles functions that interact with the Operating System.
  • The second line creates a directory named thumbnails.
  • The third line changes the current working directory to the thumbnails folder.
  • The fourth line creates a list named sizes that contains three elements ‘small’, ‘medium’, and ‘large’.
  • The fifth and sixth lines are a for loop that iterates through the sizes list and creates three folders within the current working directory. Each folder is named after each element in the size list.
  • The seventh line prints the existing directories (in a list format) within the current working directory, which is the thumbnails folder.
  • The output is [‘large’, ‘small’, ‘medium’].

More Questions: Python Essentials 2 – Module 4 Test