Code Argument Alternative Doesn't Work?

I’m wondering about the given code:

import os

def make_folders(folders_list, nest=False):
  if nest:
    """
    Nest all the folders, like
    ./Music/fun/parliament
    """
    path_to_new_folder = ""
    for folder in folders_list:
      path_to_new_folder += "/{}".format(folder)
      try:
        os.makedirs(path_to_new_folder)
      except FileExistsError:
        continue
  else:
    """
    Makes all different folders, like
    ./Music/ ./fun/ and ./parliament/
    """
    for folder in folders_list:
      try:
        os.makedirs(folder)

      except FileExistsError:
        continue

make_folders(['./Music', './fun', './parliament'])

If the function make_folders(folders_list, nest=False) was called with nest = True and with the included example list

make_folders(['./Music', './fun', './parliament'])

It would attempt to create a path of folders (acording to this code):

    path_to_new_folder = ""
    for folder in folders_list:
      path_to_new_folder += "/{}".format(folder)
      try:
        os.makedirs(path_to_new_folder)

Which translate to path = /./Music/./fun/./parliament
and anticpiates only for except FileExistsError: ?

1 Like
import os

def make_folders(folders_list, nest=False):
  if nest:
    """
    Nest all the folders, like
    ./Music/fun/parliament
    """
    path_to_new_folder = "."
    for folder in folders_list:
      path_to_new_folder += "/{}".format(folder)
      try:
        os.makedirs(path_to_new_folder)
      except FileExistsError:
        continue
  else:
    """
    Makes all different folders, like
    ./Music/ ./fun/ and ./parliament/
    """
    for folder in folders_list:
      try:
        os.makedirs("./" + folder)
      except FileExistsError:
        continue

make_folders(['Music', 'fun', 'parliament'])
make_folders(["Music", "fun", "parliament"], True)
1 Like