用python复制文件

1. 根据文件夹的名称复制

需要复制的文件夹编号文件中,每一行表示一个编号,如下所示:

> cat id.txt
1
2
3
...
>

目标文件的目录结构树如下所示:

  • 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[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)

用python复制文件夹的更多相关文章

  1. python 中文件夹的操作

    文件有两个管家属性:路径和文件名. 路径指明了文件在磁盘的位置,文件名原点的后面部分称为扩展名(后缀),它指明了文件的类型. 一:文件夹操作 Python中os 模块可以处理文件夹 1,当前工作目录 ...

  2. 用Python复制文件的9个方法

    Python 中有许多"开盖即食"的模块(比如 os,subprocess 和 shutil)以支持文件 I/O 操作.在这篇文章中,你将会看到一些用 Python 实现文件复制的 ...

  3. 用Python复制文件的9个方法(转)

    转自:https://zhuanlan.zhihu.com/p/35725217 用Python复制文件的9个方法 Python 中有许多“开盖即食”的模块(比如 os,subprocess 和 sh ...

  4. JAVA实现复制文件夹

    package com.filetest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...

  5. CMD复制文件夹

    CMD复制文件夹 xcopy /E/I/Y "D:\GitHub\WIP\app" "D:\GitHub\WIP_server\html\webshell"

  6. python 遍历文件夹 文件

    python 遍历文件夹 文件   import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...

  7. Web 在线文件管理器学习笔记与总结(13)重命名文件夹(14)复制文件夹

    (13)重命名文件夹 ① 重命名文件夹通过 rename($oldname,$newname) 实现 ② 检测文件夹名是否符合规范 ③ 检测当前目录中是否存在同名文件夹名称,如果不存在则重命名成功 i ...

  8. c# 封装的文件夹操作类之复制文件夹

    c#  封装的文件夹操作类之复制文件夹 一.复制文件夹原理: 1.递归遍历文件夹 2.复制文件 二.FolderHelper.cs /// <summary> /// 文件夹操作类 /// ...

  9. python 关于文件夹的操作

    在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir() #  path 表示文 ...

随机推荐

  1. 手写一个简版 asp.net core

    手写一个简版 asp.net core Intro 之前看到过蒋金楠老师的一篇 200 行代码带你了解 asp.net core 框架,最近参考蒋老师和 Edison 的文章和代码,结合自己对 asp ...

  2. PAT-1059 Prime Factors (素数因子)

    1059. Prime Factors Given any positive integer N, you are supposed to find all of its prime factors, ...

  3. git rebase 还是 merge的使用场景最通俗的解释

    什么是 rebase? git rebase 你其实可以把它理解成是“重新设置基线”,将你的当前分支重新设置开始点.这个时候才能知道你当前分支于你需要比较的分支之间的差异. 原理很简单:rebase需 ...

  4. Python之日志处理(logging模块二实战)

    实战篇 import logging import logging.handlers LOG_PATH = r'./' def logConfig_1(): ''' 配置 log 输出到文件 : fi ...

  5. 【转】团队项目的Git分支管理规范

    原文地址: http://blog.jboost.cn/git-branch.html 分支管理 创建项目时(一般是服务型项目,工具型或辅助型项目可以简单一些),会针对不同环境创建三个常设分支: de ...

  6. 前端基础进阶(十一):详细图解jQuery对象,以及如何扩展jQuery插件

    早几年学习前端,大家都非常热衷于研究jQuery源码.我还记得当初从jQuery源码中学到一星半点应用技巧的时候常会有一种发自内心的惊叹,“原来JavaScript居然可以这样用!” 虽然随着前端的发 ...

  7. 脚本学习一(echo、echo off、@、start)

    1.echo表示显示此命令后的字符 脚本: 输出结果: 2.echo off表示在此语句后所有运行的命令都不显示命令行本身 脚本: 输出结果: 3.@与echo off相象,但它是加在每个命令行的最前 ...

  8. Java中的集合(五)继承Collection的List接口

    Java中的集合(五)继承Collection的List接口 一.List接口简介 List是有序的Collection的,此接口能够精确的控制每个元素插入的位置.用户能够根据索引(元素在List接口 ...

  9. JavaScript几种继承方式的总结

    1.原型链继承 直接将子类型的原型指向父类型的实例,即"子类型.prototype = new 父类型();",实现方法如下: //父类构造函数 function father(n ...

  10. 错误记录:Data too long for column 'xxx' at row 1

    错误记录:Data too long for column 'xxx' at row 1 使用Flask-sqlalchemy操作数据时报错: "Data too long for colu ...