简体   繁体   English

如何将多个子文件夹中的图片复制到一个普通文件夹中并重命名?

[英]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”包含多个子文件夹及其相关文件,如下面的树状图所示:

├── 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.可以看出,DICOM 图像文件具有相同的名称(1.dcm、2.dcm、3.dcm、...)但它们属于不同的患者。 The sub-folders "Subfolder_with_patientID1", "Subfolder_with_patientID2", "Subfolder_with_patientID3" have different folder names specific to that patient.子文件夹“Subfolder_with_patientID1”、“Subfolder_with_patientID2”、“Subfolder_with_patientID3”具有特定于该患者的不同文件夹名称。 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, ie, like the name of the Subfolder_with_patientID1_1.dcm, the name of the Subfolder_with_patientID1_2.dcm and so on.我想将所有这些 DICOM 图像复制到目标目录,同时重命名它们,以便每个图像名称附加该患者特定的子文件夹名称,即 Subfolder_with_patientID1_1.dcm 的名称、Subfolder_with_patientID1_2.dcm 的名称等在。 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))
  

Using shutil.copy2 to copy the files.使用shutil.copy2复制文件。

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

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? 如何在不指定子文件夹名称的情况下自动合并来自多个子文件夹的多个 CSV? - How to automatically merge multiple CSVs from multiple sub-folders, without specifying sub-folder name? 如何打印文件夹/子文件夹并从中导入文件? - How can I print folder/sub-folders and import files from them? 如何将特定文件从子文件夹复制到python中的新文件夹? - How to copy specific files from the sub-folders to a new folder in python? 如何从文件夹(包括子文件夹)的图像创建 Numpy 数组 - How to create Numpy Arrays from images of a folder including sub-folders 如何从多个子文件夹中随机选择 select 文件 - How to select file randomly from multiple sub-folders 如何遍历多个子文件夹并基于特定的XML标签重命名文件? - How to loop over multiple sub-folders and rename files based on a specific XML tag? 如何使用子文件夹模板在 cookiecutter 模板中创建多个子文件夹 - How to create multiple sub-folders in cookiecutter template using a sub-folder template 重命名子文件夹python中文件名的一部分 - Rename part of filenames in sub-folders python 如何编译位于带有子文件夹的文件夹中的 Python 软件 - How to compile my Python software that's in a folder with sub-folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM