用python复制文件
1. 根据文件夹的名称复制
需要复制的文件夹编号文件中,每一行表示一个编号,如下所示:
> cat id.txt
1
2
3
...
>
目标文件的目录结构树如下所示:
- Normal_data
- T1Img
- 23XIAOHEI
- 432XIAOMING
- T1ImgSegment
- 23XIAOHEI
- 432XIAOMING
- T1ImgSegmentS
- 23XIAOHEI
- 432XIAOMING
- T1Raw
- 23XIAOHEI
- 432XIAOMING
- T1Img
主要流程就是先从文件中读到要复制的文件的编号,然后遍历目标文件夹,从文件夹名称中切分出编号,然后进行复制操作。完整的代码如下:
# -*- coding: utf-8 -*-
# @Time : 2018/6/6 20:33
# @Author : sangf
# @desc : copy the t1 image by id
# if you want to know which id is not found, you should input the command 'python3 copyT1ById.py >> not_found.txt' in shell.
# And you will find the new file named 'not_found.txt' in which there are maybe some ids or not.
# If it is empty, all image have been found; and if not, those is not be found.
# Good luck!
import os
import shutil
import re
# must set those value
SRC_PATH = r'/home/admin/MRI_DATA/T1/Normal_data'
DST_PATH = r'/home/admin/Desktop/xxx'
ID_FILE_PATH = r'/home/admin/MRI_DATA/T1/xxx.txt'
TYPE = r'T1Raw'
def cutIdInFloderName(floderName):
'''
' cut out the id in floderName.
' Don't change this function.
'''
idIndex = floderName.index(re.search(r'[A-Za-z]', floderName).group())
id = floderName[0:idIndex]
return id
def