Autosplit
diff --git a/utils/datasets.py b/utils/datasets.py
index 7466ba48b27..eb355e913b8 100755
--- a/utils/datasets.py
+++ b/utils/datasets.py
@@ -902,3 +902,20 @@ def flatten_recursive(path='../coco128'):
     create_folder(new_path)
     for file in tqdm(glob.glob(str(Path(path)) + '/**/*.*', recursive=True)):
         shutil.copyfile(file, new_path / Path(file).name)
+
+
+def autosplit(path='../coco128', weights=(0.9, 0.1, 0.0)):  # from utils.datasets import *; autosplit()
+    """ Autosplit a dataset into train/val/test splits and save *.txt files
+    # Arguments
+        path:       Path to images directory
+        weights:    Train, val, test weights (list)
+    """
+    path = Path(path)  # images dir
+    files = list(path.rglob('*.*'))
+    indices = random.choices([0, 1, 2], weights=weights, k=len(files))  # assign each image to a split
+    txt = ['autosplit_train.txt', 'autosplit_val.txt', 'autosplit_test.txt']  # 3 txt files
+    [(path / x).unlink() for x in txt if (path / x).exists()]  # remove existing
+    for i, img in tqdm(zip(indices, files)):
+        if img.suffix[1:] in img_formats:
+            with open(path / txt[i], 'a') as f:
+                f.write(str(img) + '\n')  # add image to txt file

This PR implements an Autosplit function addition. This new function automatically splits a dataset into train/val/test splits.

## 🛠️ PR Summary

<sub>Made with ❤️ by [Ultralytics Actions](https://github.com/ultralytics/actions)<sub>

### 🌟 Summary
Introducing an automatic dataset splitting function to the YOLOv5 repository.

### 📊 Key Changes
- Added the `autosplit` function within `datasets.py`.
- The function divides a dataset into training, validation, and test sets based on specified weights.
- It generates three text files (`autosplit_train.txt`, `autosplit_val.txt`, `autosplit_test.txt`) to store lists of the file paths for each split.
- Existing split text files are removed before creating new ones to avoid duplication.

### 🎯 Purpose & Impact
- **Ease of Use:** Simplifies the process of creating train/val/test splits, making it easier for users to prepare their datasets.
- **Flexibility:** Allows users to define their own split ratios through the `weights` parameter.
- **Efficiency:** Automatically handles the creation and cleaning up of split files, saving time and reducing manual errors.
- **Potential Impact:** Could improve overall data management practices and reproducibility of experiments for YOLOv5 users. 🚀