code: def flatten_dir(top_dir):
      # Collect directories to delete
      directories_to_delete = []
  
      # Walk through all subdirectories
      for foldername, subfolders, filenames in os.walk(
          top_dir, topdown=False
      ):
          # If we are not in the top-level directory, move files to the top-level directory
          if foldername != top_dir:
              for filename in filenames:
                  source = os.path.join(foldername, filename)
                  destination = os.path.join(top_dir, filename)
  
                  # Check if a file with the same name already exists in the top-level directory
                  if os.path.exists(destination):
                      base, ext = os.path.splitext(filename)
                      counter = 1
                      new_destination = os.path.join(
                          top_dir, f"{base}_{counter}{ext}"
                      )
  
                      # Find a new destination path that does not exist yet
                      while os.path.exists(new_destination):
                          counter += 1
                          new_destination = os.path.join(
                              top_dir, f"{base}_{counter}{ext}"
                          )
  
                      destination = new_destination
  
                  # Move file
                  shutil.move(source, destination)
                  print(f"Moved: {source} to {destination}")
  
              # Add the foldername to the list of directories to delete
              directories_to_delete.append(foldername)
  
      # Delete the old subdirectories
      for directory in directories_to_delete:
          os.rmdir(directory)
          print(f"Deleted: {directory}")
  
imports: import getpass
  import os
  import shutil
  import glob
label_desc: Use image node_id 2 for calling a global function with example usage: flatten_dir(top_dir) without return types