standardize_filename
standardize_filename
Functions
| Name | Description |
|---|---|
| standardize_filename | Standardize filenames in a directory by normalizing case, separators, |
standardize_filename
standardize_filename.standardize_filename(dir=None, case='lower', sep='_')Standardize filenames in a directory by normalizing case, separators, and duplicate punctuation.
This function iterates over all files in the specified directory and renames them according to the selected formatting rules. Directory structure is preserved; only filenames are modified.
The following transformations are applied: 1. Filename case can be converted to title case, upper case, or lower case. Case transformation is applied only to the filename stem; file extensions are preserved. 2. Hyphens (-), underscores (_), and spaces are treated as word separatores and normalized to sep. 3. Duplicate punctuation characters (e.g., ’__‘,’–‘,’ ’, etc.) are reduced to a single instance.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| dir | str or Path | Path to the directory containing files to be standardized. | None |
| case | (title, upper, lower) | Desired casing for filenames (default is ‘lower’). | 'title' |
| sep | ('-', _, ' ') | Character to use as the separator between words in filenames (default is ’_’). | '-' |
Returns
| Name | Type | Description |
|---|---|---|
| list of tuple(Path, Path) | A list of (old_path, new_path) pairs for each file that was renamed. Files whose names were unchanged are not included. |
Examples
>>> standardize_filename("data/", case="title", sep="-")
Renames files like:
"my__sample file--name.txt" -> "My-Sample-File-Name.txt"