code: def create_data_file_by_format(directory_path = None):
      
      stata_files = []
      sas_files = []
  
      for root, subdirs, files in os.walk(directory_path):
          for file_name in files:
              file_path = os.path.join(root, file_name)
              if os.path.basename(file_name).endswith(".dta"):
                  log.info("Found stata file {}".format(file_path))
                  stata_files.append(file_path)
              if os.path.basename(file_name).endswith(".sas7bdat"):
                  log.info("Found sas file {}".format(file_path))
                  sas_files.append(file_path)
      return {'stata': stata_files, 'sas': sas_files}
docs: Browse subdirectories to extract stata and sas files