I have .dcm files in a path like below (old_path);
old_path: Mass-Test_P_00016_LEFT_MLO_1/10-04-2016-DDSM-15563/1-cropped images-77287/000000.dcm I have to rename the path like below (good_path);
good_path: Mass-Test_P_00016_LEFT_MLO_1/15563/77287/000000.dcm Note that I keep only the last 5 digits in the sub-folders as shown above.
Please anyone show me how this must be done? This is my attempt...
os.chdir(path to data) os.listdir() >> ['Mass-Test_P_00016_LEFT_MLO_1', 'Mass-Test_P_00016_LEFT_MLO'] temp = os.walk('Mass-Test_P_00016_LEFT_MLO_1', topdown=False) for root, dirs, files in temp: for name in files: old_path = os.path.join(root, name) print("old_path: ", old_path) first = old_path.split('/')[1][-5:] second = old_path.split('/')[2][-5:] #print(first, second) good_path = os.path.join(old_path.split('/')[0], first, second, old_path.split('/')[3]) print("good_path: ", good_path) os.rename(old_path, good_path) I was able to set the good_path as I want. But it is not overwriting the subfolders names.
os.path.split()to split paths (instead of e.g.old_path.split('/')).old_path.split('/')to a variable so you don't have to keep calling it.