How to rename all specific filetypes of files within subfolders to name with same file extension but same file name
The "newer" pathlib module is helpful here.
A recursive glob can match all filenames by extension.
>>> from pathlib import Path
>>> for p in Path().rglob('*.iso'):
... print(p)
a/b/2.iso
a/b/c/d/3.iso
a/b/c/d/e/1.iso
The part before the extension is called the stem
in pathlib terminology - you can modify this using .with_stem()
>>> for p in Path().rglob('*.iso'):
... print(p.with_stem('game'))
a/b/game.iso
a/b/c/d/game.iso
a/b/c/d/e/game.iso
You can pass this directly to the .rename()
method
for p in Path().rglob('*.iso'):
p.rename(p.with_stem('game'))
Which will rename all of the files:
>>> for p in Path().rglob('*.iso'):
... print(p)
a/b/game.iso
a/b/c/d/game.iso
a/b/c/d/e/game.iso