title: How to wait for the last function to complete
body: <p>I'm working on a program to sort all my image files into folders by year and then by month.
  I've written a series of functions that, when run individually, will complete the task.</p>
  <p>My problem is, if i run these functions straight after each other, it wont work. It simply looks like the rearranging of files on my os is happening so slowly, that my program will outrun it and cause 'No such file or directory' errors because some folders hasn't been created yet.</p>
  <p><strong>How can i make my program wait for the last function and continue when it is completed?</strong></p>
  <p>I thought of simply sleep()ing it off, but that feels like a half solution since the time could vary based on the size of my data set.</p>
  <pre><code>import time
  import os
  import shutil
  from os.path import join
  
  #path = &quot;C:/Users/caspe/Desktop/Iphone Sync/jpg&quot;
  path = &quot;C:/Users/caspe/Desktop/TEST&quot;
  list_ = os.listdir(path)
  increment = 0
  
  database = {}
  
  &quot;&quot;&quot;Create a database of all the files, to be able to work around duplicate dates.&quot;&quot;&quot;
  def create_db():
      new_database = {}
      for file_ in list_:
          path_to_file = join(path, file_)
          t = os.path.getmtime(path_to_file)
          t_str = time.ctime(t)
          t_obj = time.strptime(t_str)
          form_t = time.strftime(&quot;%d-%m-%Y %H:%M&quot;, t_obj)
          form_t = form_t.replace(&quot;:&quot;, &quot;꞉&quot;)
          #print (form_t)
          #print (t)
          name, ext = os.path.splitext(file_)
          ext = ext[1:]
      
          if ext == '':
              continue
          else:
              if form_t in new_database:
                  new_database[form_t] += 1
              else:
                  new_database[form_t] = 0
  
      #print(new_database)
      return new_database
  
  &quot;&quot;&quot;Do a simple reset of all filenames equal to the number of an incrementing integer.&quot;&quot;&quot;
  def reset_names():
      i = 0
      for file_ in list_:
          path_to_file = join(path, file_)
          os.rename(path + &quot;/&quot; + file_, os.path.split(path_to_file)[0] + &quot;/&quot; + str(i) + os.path.splitext(path_to_file)[1])
          i += 1
  
  &quot;&quot;&quot;Rename all files based on date.&quot;&quot;&quot;
  def rename_files():
      for file_ in list_:
          path_to_file = join(path, file_)
          t = os.path.getmtime(path_to_file)
          t_str = time.ctime(t)
          t_obj = time.strptime(t_str)
          form_t = time.strftime(&quot;%d-%m-%Y %H:%M&quot;, t_obj)
          form_t = form_t.replace(&quot;:&quot;, &quot;꞉&quot;)
  
          if database.keys().__contains__(form_t):
              new_name = os.path.split(path_to_file)[0] + '/' + form_t + &quot;-&quot; + str(database[form_t]) + os.path.splitext(path_to_file)[1]
              database[form_t] -= 1
              os.rename(path_to_file, new_name)
          else:
              print(&quot;no images found&quot;)
              #continue
  
  &quot;&quot;&quot;Sort all files into folders based on year.&quot;&quot;&quot;
  def sort_files_by_year():
      year_folders = []
  
      # Sort all files into folders based on year
      for file_ in list_:
          #path_to_file = join(path, file_)
          name, ext = os.path.splitext(file_)
          month = name[3:5]
          year = name[6:10]
          ext = ext[1:]
          #print(month)
          #print(year)
          
          # folder 2021
          #   - folder for each month
          #       - all pics in correct &quot;month&quot; folder
  
          # We do not want folders, so skip it
          if ext == '':
              continue
  
          if os.path.exists(path + &quot;/&quot; + year):
              shutil.move(path + &quot;/&quot; + file_, path + &quot;/&quot; + year + &quot;/&quot; + file_)
          else:
              new_year_folder = os.makedirs(path + &quot;/&quot; + year)
              year_folders.append(new_year_folder)
              shutil.move(path + &quot;/&quot; + file_, path + &quot;/&quot; + year + &quot;/&quot; + file_)
      
  &quot;&quot;&quot;Sort the contents of each &quot;year&quot; folder, into &quot;month&quot; folders.&quot;&quot;&quot;
  def sort_each_year_folder_by_month():
      # Loop through each &quot;year&quot; folder
      # Inside each &quot;year&quot; sort files based on month
      for folder in list_:
          name, ext = os.path.splitext(folder)
          print (name)
          new_path = path + &quot;/&quot; + name
          new_list = os.listdir(new_path)
  
          # We only want folders
          if ext != '':
              continue
  
          for file_ in new_list:
              name, ext = os.path.splitext(file_)
              month = name[3:5]
              #ext = ext[1:]
  
              # We do not want folders, so skip it
              if ext == '':
                  continue
              
              if os.path.exists(new_path + &quot;/&quot; + month):
                  shutil.move(new_path + &quot;/&quot; + file_, new_path + &quot;/&quot; + month + &quot;/&quot; + file_)
              else:
                  os.makedirs(new_path + &quot;/&quot; + month)
                  shutil.move(new_path + &quot;/&quot; + file_, new_path + &quot;/&quot; + month + &quot;/&quot; + file_)
  
      
  
  #
  
  &quot;&quot;&quot;Step 1.&quot;&quot;&quot;
  #reset_names()
  
  &quot;&quot;&quot;Step 2.&quot;&quot;&quot;
  #database = create_db()
  #rename_files()
  
  &quot;&quot;&quot;Step 3.&quot;&quot;&quot;
  #sort_files_by_year()
  #sort_each_year_folder_by_month()
  </code></pre>