Rename multiple files inside multiple folders

前端 未结 3 540
一整个雨季
一整个雨季 2021-01-24 01:50

So I have a lot of folders with a certain name. In each folder I have +200 items. The items inside the folders has names like:

CT.34562346.246.dcm
RD.34562346.dc         


        
3条回答
  •  不思量自难忘°
    2021-01-24 02:11

    Split each file name on the . and replace the second item with the file name, then join on .'s again for the new file name. Here's some sample code that demonstrates the concept.

    folder_name = ['1', '2']
    
    file_names = ['CT.2345.234.dcm', 'BG.234234.222.dcm', "RA.3342.221.dcm"]
    
    
    for folder in folder_name:
        new_names = []
        for x in file_names:
            file_name = x.split('.')
            file_name[1] = folder
            back_together = '.'.join(file_name)
            new_names.append(back_together)
    
        print(new_names)
    

    Output

    ['CT.1.234.dcm', 'BG.1.222.dcm', 'RA.1.221.dcm']
    ['CT.2.234.dcm', 'BG.2.222.dcm', 'RA.2.221.dcm']
    

提交回复
热议问题