How to rename all files in a directory in Python

Renaming all files in a directory changes all filenames in a folder. The new filenames can follow a template or pattern, but each file must have a unique name.

Solution for How to rename all files in a directory in Python : You can use pathlib.Path.iterdir() to rename all files in a directory Iterate through the contents of pathlib.Path(directory).iterdir() with a for-loop to access each file path in directory. At each iteration, check if the path is a file with pathlib.Path.is_file(). If it is, call pathlib.Path.rename(new) with new as a pathlib.Path object with the new filename.


how-to-rename-all-files-in-a-directory-in-python