天天看點

用python複制檔案夾

用python複制檔案

1. 根據檔案夾的名稱複制

需要複制的檔案夾編号檔案中,每一行表示一個編号,如下所示:

> cat id.txt
  
  
  
  ...
>
           

目标檔案的目錄結構樹如下所示:

  • Normal_data
    • T1Img
      • 23XIAOHEI
      • 432XIAOMING
    • T1ImgSegment
      • 23XIAOHEI
      • 432XIAOMING
    • T1ImgSegmentS
      • 23XIAOHEI
      • 432XIAOMING
    • T1Raw
      • 23XIAOHEI
      • 432XIAOMING

主要流程就是先從檔案中讀到要複制的檔案的編号,然後周遊目标檔案夾,從檔案夾名稱中切分出編号,然後進行複制操作。完整的代碼如下:

# -*- 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[:idIndex]
    return id

def indexDict(srcPath, typeData):
    '''
    ' building the index dict.
    ' example: {path, id}.
    ' Don't change this function.
    '''
    tmpIndexDict = {}
    for tmpYearFloder in os.listdir(srcPath):
        tmpYearFloderPath = os.path.join(srcPath, tmpYearFloder)
        tmpTypeFloderPath = os.path.join(tmpYearFloderPath, typeData)
        for tmpSubFloder in os.listdir(tmpTypeFloderPath):
            tmpSubFloderPath = os.path.join(tmpTypeFloderPath, tmpSubFloder)
            tmpIndexDict[tmpSubFloderPath] = cutIdInFloderName(tmpSubFloder)
        # end for
    # end for
    return tmpIndexDict


def findPathInDict(tmpIndexDict, tmpId):
    '''
    ' find the path from indexDict.
    ' if not found, the size of return is 0
    ' Please don't change the function.
    '''
    tmpFindedPath = []
    for tmpKey in tmpIndexDict.keys():
        if tmpIndexDict[tmpKey] == tmpId:
            tmpFindedPath.append(tmpKey)
        # end if
    # end for
    return tmpFindedPath


def main(tmpSrcPath, tmpDstPath, tmpIdFilePath, tmpType):
    '''
    ' the main function.
    ' this function is the controller of the program.
    ' so it is very import to keep this function is not be changed.
    ' lol...
    '''
    idList = []
    with open(tmpIdFilePath, 'r') as f:
        for line in f.readlines():
            line = line.replace('\n', '')
            # print(line)
            # avoid the same id in id list
            try:
                idList.index(line)
            except ValueError:
                idList.append(line)
        # end for
    # end open
    # build index
    indexs = indexDict(tmpSrcPath, tmpType)
    # find the path
    for tmpId in idList:
        paths = findPathInDict(indexs, tmpId)
        if len(paths) == :
            # print not found
            print(tmpId)
        else:
            # copy
            for tmpPath in paths:
                tmpSplitPath = tmpPath.split('/')
                tmpDstCmpltPath = os.path.join(tmpDstPath, tmpSplitPath[-], tmpSplitPath[-], tmpSplitPath[-])
                # print(tmpDstCmpltPath)
                shutil.copytree(tmpPath, tmpDstCmpltPath)
        # end if
    # end for


# the start of the program
main(SRC_PATH, DST_PATH, ID_FILE_PATH, TYPE)
           

2. 根據檔案夾的名稱複制并重命名

流程與上述流程類似,代碼如下:

# -*- coding: utf-8 -*-
# @Time    : 2018/6/6 20:33
# @Author  : sangf
# @desc    : copy the t1 image by id, and rename the floder
#            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/Desktop/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[:idIndex]
    return id

def indexDict(srcPath, typeData):
    '''
    ' building the index dict.
    ' example: {path, id}.
    ' Don't change this function.
    '''
    tmpIndexDict = {}
    for tmpYearFloder in os.listdir(srcPath):
        tmpYearFloderPath = os.path.join(srcPath, tmpYearFloder)
        tmpTypeFloderPath = os.path.join(tmpYearFloderPath, typeData)
        for tmpSubFloder in os.listdir(tmpTypeFloderPath):
            tmpSubFloderPath = os.path.join(tmpTypeFloderPath, tmpSubFloder)
            tmpIndexDict[tmpSubFloderPath] = cutIdInFloderName(tmpSubFloder)
        # end for
    # end for
    return tmpIndexDict


def findPathInDict(tmpIndexDict, tmpId):
    '''
    ' find the path from indexDict.
    ' if not found, the size of return is 0
    ' Please don't change the function.
    '''
    tmpFindedPath = []
    for tmpKey in tmpIndexDict.keys():
        if tmpIndexDict[tmpKey] == tmpId:
            tmpFindedPath.append(tmpKey)
        # end if
    # end for
    return tmpFindedPath


def main(tmpSrcPath, tmpDstPath, tmpIdFilePath, tmpType):
    '''
    ' the main function.
    ' this function is the controller of the program.
    ' so it is very import to keep this function is not be changed.
    ' lol...
    '''
    idList = []
    with open(tmpIdFilePath, 'r') as f:
        for line in f.readlines():
            line = line.replace('\n', '')
            # print(line)
            # avoid the same id in id list
            try:
                idList.index(line)
            except ValueError:
                idList.append(line)
        # end for
    # end open
    # build index
    indexs = indexDict(tmpSrcPath, tmpType)
    # find the path
    for tmpId in idList:
        oldIdInLine, newIdInLine = tmpId.split(',')
        paths = findPathInDict(indexs, oldIdInLine)
        if len(paths) == :
            # print not found
            print(oldIdInLine)
            # pass
        else:
            # copy
            postfix = 
            for tmpPath in paths:
                tmpSplitPath = tmpPath.split('/')
                if len(paths) > :
                    newIdInLine = newIdInLine.split('-')[] + '-' + str(postfix)
                    postfix += 
                tmpDstCmpltPath = os.path.join(tmpDstPath, tmpSplitPath[-], newIdInLine)
                # print(tmpDstCmpltPath)
                shutil.copytree(tmpPath, tmpDstCmpltPath)
        # end if
    # end for


# the start of the program
main(SRC_PATH, DST_PATH, ID_FILE_PATH, TYPE)