用python复制文件夹
用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 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) == 0:
# print not found
print(tmpId)
else:
# copy
for tmpPath in paths:
tmpSplitPath = tmpPath.split('/')
tmpDstCmpltPath = os.path.join(tmpDstPath, tmpSplitPath[-3], tmpSplitPath[-2], tmpSplitPath[-1])
# 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[0: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) == 0:
# print not found
print(oldIdInLine)
# pass
else:
# copy
postfix = 1
for tmpPath in paths:
tmpSplitPath = tmpPath.split('/')
if len(paths) > 1:
newIdInLine = newIdInLine.split('-')[0] + '-' + str(postfix)
postfix += 1
tmpDstCmpltPath = os.path.join(tmpDstPath, tmpSplitPath[-2], 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)