shiva
shiva

Reputation: 1189

How to copy images from multiple sub-folders to a common folder and rename them?

Lets assume the following source and destination paths:

dir_src = r"/path/to/folder_with_subfolders"
dir_dst = r"/path/to/destination_folder" 

The directory "folder_with_subfolders" contains multiple sub-folders and their associated files as shown in the tree diagram below:

├── folder_with_subfolders
│   ├── Subfolder_1
|   |   ├── Subfolder1_1
|   |   |   ├──Subfolder_with_patientID1
|   |   |   |   ├── 1.dcm
|   |   |   |   ├── 2.dcm
|   |   |   |   ├── 3.dcm
                    .......
│   ├── Subfolder_2
|   |   ├── Subfolder2_1
|   |   |   ├──Subfolder_with_patientID2
|   |   |   |   ├── 1.dcm
|   |   |   |   ├── 2.dcm
                    .......
│   ├── Subfolder_3
|   |   ├── Subfolder3_1
|   |   |   ├──Subfolder_with_patientID3
|   |   |   |   ├── 1.dcm
|   |   |   |   ├── 2.dcm
|   |   |   |   ├── 3.dcm
|   |   |   |   ├── 4.dcm
                    .......

It can be seen that the DICOM image files have the same names (1.dcm, 2.dcm, 3.dcm, ...) but they belong to different patients. The sub-folders "Subfolder_with_patientID1", "Subfolder_with_patientID2", "Subfolder_with_patientID3" have different folder names specific to that patient. I want to copy all these DICOM images to the destination directory while renaming them such that each image name is appended with that patient-specific subfolder name, i.e., like the name of the Subfolder_with_patientID1_1.dcm, the name of the Subfolder_with_patientID1_2.dcm and so on. This is the starting script:

import glob
import os

# Location with subdirectories
dir_src = r"/path/to/folder_with_subfolders/"
#destination loction to move all the files
dir_dst = r"/path/to/destination/"    
# Get List of all images
files = glob.glob(dir_src + '/**/**/**/*.dcm', recursive=True)     
for file in files:
    # Get File name and extension
    filename = os.path.basename(file)
    filename=filename[:-4].replace(".",.........) + ".dcm"
    # Copy the file with os.rename
    if not os.path.exists(os.path.join(dir_dst, filename)):
        os.rename(file, os.path.join(dir_dst, filename))
  

Upvotes: 0

Views: 1444

Answers (2)

Niel Godfrey P. Ponciano
Niel Godfrey P. Ponciano

Reputation: 10699

Using shutil.copy2 to copy the files.

import glob
import os
import shutil

# Location with subdirectories
dir_src = "/home/nponcian/Documents/folder_with_subfolders/"

# Destination location to copy all the files
dir_dst = "/home/nponcian/Documents/folder_with_subfolders_dest/"

# Get List of all images
files = glob.glob(dir_src + '/**/*.dcm', recursive=True)

# Create the destination directory
if not os.path.exists(dir_dst):
    os.mkdir(dir_dst)

# For each image
for file_name_src in files:
    # Let's say file_name_src is currently "/home/nponcian/Documents/folder_with_subfolders/Subfolder_1/Subfolder1_1/Subfolder_with_patientID1/2.dcm"

    file_dir = os.path.basename(os.path.dirname(file_name_src))  # Would be "Subfolder_with_patientID1"
    file_name = os.path.basename(file_name_src)  # Would be "2.dcm"

    file_name_dst = os.path.join(dir_dst, f"{file_dir}_{file_name}")  # Would be "/home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID1_2.dcm"

    shutil.copy2(file_name_src, file_name_dst)
    print(f"Copied:\n\tFr: {file_name_src}\n\tTo: {file_name_dst}")

File tree before the script was run

$ tree
.
├── folder_with_subfolders
│   ├── Subfolder_1
│   │   └── Subfolder1_1
│   │       └── Subfolder_with_patientID1
│   │           ├── 1.dcm
│   │           ├── 2.dcm
│   │           └── 3.dcm
│   ├── Subfolder_2
│   │   └── Subfolder2_1
│   │       └── Subfolder_with_patientID2
│   │           ├── 1.dcm
│   │           └── 2.dcm
│   └── Subfolder_3
│       └── Subfolder3_1
│           └── Subfolder_with_patientID3
│               ├── 1.dcm
│               ├── 2.dcm
│               ├── 3.dcm
│               └── 4.dcm
└── script.py

10 directories, 10 files

File tree after the script was run

$ tree
.
├── folder_with_subfolders
│   ├── Subfolder_1
│   │   └── Subfolder1_1
│   │       └── Subfolder_with_patientID1
│   │           ├── 1.dcm
│   │           ├── 2.dcm
│   │           └── 3.dcm
│   ├── Subfolder_2
│   │   └── Subfolder2_1
│   │       └── Subfolder_with_patientID2
│   │           ├── 1.dcm
│   │           └── 2.dcm
│   └── Subfolder_3
│       └── Subfolder3_1
│           └── Subfolder_with_patientID3
│               ├── 1.dcm
│               ├── 2.dcm
│               ├── 3.dcm
│               └── 4.dcm
├── folder_with_subfolders_dest
│   ├── Subfolder_with_patientID1_1.dcm
│   ├── Subfolder_with_patientID1_2.dcm
│   ├── Subfolder_with_patientID1_3.dcm
│   ├── Subfolder_with_patientID2_1.dcm
│   ├── Subfolder_with_patientID2_2.dcm
│   ├── Subfolder_with_patientID3_1.dcm
│   ├── Subfolder_with_patientID3_2.dcm
│   ├── Subfolder_with_patientID3_3.dcm
│   └── Subfolder_with_patientID3_4.dcm
└── script.py

11 directories, 19 files

Logs

$ python3 script.py 
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_2/Subfolder2_1/Subfolder_with_patientID2/2.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID2_2.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_2/Subfolder2_1/Subfolder_with_patientID2/1.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID2_1.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_1/Subfolder1_1/Subfolder_with_patientID1/3.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID1_3.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_1/Subfolder1_1/Subfolder_with_patientID1/2.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID1_2.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_1/Subfolder1_1/Subfolder_with_patientID1/1.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID1_1.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_3/Subfolder3_1/Subfolder_with_patientID3/3.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID3_3.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_3/Subfolder3_1/Subfolder_with_patientID3/4.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID3_4.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_3/Subfolder3_1/Subfolder_with_patientID3/2.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID3_2.dcm
Copied:
    Fr: /home/nponcian/Documents/folder_with_subfolders/Subfolder_3/Subfolder3_1/Subfolder_with_patientID3/1.dcm
    To: /home/nponcian/Documents/folder_with_subfolders_dest/Subfolder_with_patientID3_1.dcm

Upvotes: 1

Tom Murley
Tom Murley

Reputation: 994

You will want to split out the subfolder:

subfolder = os.path.dirname(file).split('/')[-1]

Then add the subfolder into your rename:

os.rename(file, dir_dst + subfolder + '_' + filename)

So the section should look something like this:

import glob
import os

# Location with subdirectories
dir_src = r"/path/to/folder_with_subfolders/"
#destination loction to move all the files
dir_dst = r"/path/to/destination/"    
# Get List of all images
files = glob.glob(dir_src + '/**/**/**/*.dcm', recursive=True)     
for file in files:
    # Get File name and extension
    filename = os.path.basename(file)
    subfolder = os.path.dirname(file).split('/')[-1]
    # Copy the file with os.rename
    if not os.path.exists(os.path.join(dir_dst, filename)):
         os.rename(file, dir_dst + subfolder + '_' + filename)

Upvotes: 0

Related Questions