flatten
flatten
Functions
| Name | Description |
|---|---|
| flatten | Flatten a directory by moving all files from nested subdirectories |
flatten
flatten.flatten(nested_directory, output_directory=None, recursive=False)Flatten a directory by moving all files from nested subdirectories into a target directory.
By default, only files directly inside ‘nested_directory’ are moved. If ‘recursive’ is True, files from all nested subdirectories are also moved. If any naming conflicts occur, files are renamed by appending a counter to their base names.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| nested_directory | str | Root directory containing files and nested subdirectories to flatten. | required |
| output_directory | str | Directory where flattened files will be moved. If None, the current working directory is used. | None |
| recursive | bool | If True, flatten files from all nested subdirectories. If False, only flatten files directly under nested_directory. |
False |
Returns
| Name | Type | Description |
|---|---|---|
| bool | True if the operation was successful, False otherwise. |
Examples
Flatten only top-level files:
>>> flatten("./assignments", "./")
TrueFlatten all nested files:
>>> flatten("./images", recursive=True)
True