the file location is: /Users/D/Desktop/Files/1/no.h5 and its the same filename (no.h5) in the folders 1-400. I want all these files to collect in the same folder with their number as their new names.
Your help is greatly appreciated!
You could use a glob pattern to find the target files to do the move. The pathlib library provides a convenient way to manage the paths.
from pathlib import Path
base = Path(" /Users/D/Desktop/Files")
target_folder = Path("/some/where/else")
for target in base.glob("*/no.h5"):
name = target.parent.name
if name.isdigit():
target.rename(target_folder/(name + ".h5"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With