title: How to create a folder to store files generated in a loop?
body: <p>I need to do the following in my program:</p>
  
  <p>a) Ask the user to enter the name of a folder to be created in my directory
  b) create the folder
  c) generate the files inside the folder</p>
  
  <p>The files to be generated are different texts extracted from a book using REGEX. I want the program to loop throughout the REGEX matches and automatically put each match in a file (each text matched in a file). These text files need to be stored in this newly created folder named by the user. </p>
  
  <p>My program creates the folder but only the first text was written to a file inside this new folder. It seems that the error is in the loop. The warning message is:</p>
  
  <blockquote>
    <p>Chapter1.txt Traceback (most recent call last):   File
    "/Users/nataliaresende/Dropbox/PYTHON/word_concatenator_final.py",
    line 375, in 
        menu()   File "/Users/nataliaresende/Dropbox/PYTHON/word_concatenator_final.py",
    line 365, in menu
        separate_chapters()   File "/Users/nataliaresende/Dropbox/PYTHON/word_concatenator_final.py",
    line 224, in separate_chapters
        os.makedirs(folder)   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py",
    line 221, in makedirs
        mkdir(name, mode) FileExistsError: [Errno 17] File exists: '/Users/nataliaresende/Dropbox/PYTHON/folder'</p>
  </blockquote>
  
  <p>Does anyone have an idea of what's going on? Maybe identation? </p>
  
  <pre><code>us=input('\n\nEnter the name of the book or file: ')
  
      pat = re.compile(r'(?&lt;=\[@CHAPST@\]).+?(?=\[@CHAPFN@\])')
      my_chapters=[]
  
  
      with open(us, 'r', encoding="utf-8") as file:
          for i in pat.findall(file.read()):
              my_chapters.append(i)
  
      print('\n\nThis book contains',len(my_chapters), 'chapters')
  
      user2=input('\n\nWrite chapters to a file? | Y/N: ')
  
  
      if user2 =='Y':
      folder_name=input("Name your folder. E.g. 'Texts': ")
      folder=os.path.join('/Users/nataliaresende/Dropbox/PYTHON/', folder_name)
      os.makedirs(folder)
  
      for j in range(len(my_chapters)):
          chap='Chapter'+str(j+1) +'.txt'.format(folder_name)
          file = os.path.join(folder, chap)
  
          with open(file, "w") as f:
              for item in my_chapters[j]:
                  f.write("%s" % str(item))
          print('\n\n', chap)
  
  print('\n\nChapters 1 -',len(my_chapters), 'written to a file separately. Check your folder named "Chapters" in your directory')
  </code></pre>