Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a specific file from hundreds of folders to move to one folder and rename it?

Tags:

python

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!

like image 463
helloitsmontie Avatar asked Nov 18 '25 15:11

helloitsmontie


1 Answers

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"))
like image 123
tdelaney Avatar answered Nov 21 '25 04:11

tdelaney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!