用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)
用python复制文件夹的更多相关文章
- python 中文件夹的操作
文件有两个管家属性:路径和文件名. 路径指明了文件在磁盘的位置,文件名原点的后面部分称为扩展名(后缀),它指明了文件的类型. 一:文件夹操作 Python中os 模块可以处理文件夹 1,当前工作目录 ...
- 用Python复制文件的9个方法
Python 中有许多"开盖即食"的模块(比如 os,subprocess 和 shutil)以支持文件 I/O 操作.在这篇文章中,你将会看到一些用 Python 实现文件复制的 ...
- 用Python复制文件的9个方法(转)
转自:https://zhuanlan.zhihu.com/p/35725217 用Python复制文件的9个方法 Python 中有许多“开盖即食”的模块(比如 os,subprocess 和 sh ...
- JAVA实现复制文件夹
package com.filetest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...
- CMD复制文件夹
CMD复制文件夹 xcopy /E/I/Y "D:\GitHub\WIP\app" "D:\GitHub\WIP_server\html\webshell"
- python 遍历文件夹 文件
python 遍历文件夹 文件 import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...
- Web 在线文件管理器学习笔记与总结(13)重命名文件夹(14)复制文件夹
(13)重命名文件夹 ① 重命名文件夹通过 rename($oldname,$newname) 实现 ② 检测文件夹名是否符合规范 ③ 检测当前目录中是否存在同名文件夹名称,如果不存在则重命名成功 i ...
- c# 封装的文件夹操作类之复制文件夹
c# 封装的文件夹操作类之复制文件夹 一.复制文件夹原理: 1.递归遍历文件夹 2.复制文件 二.FolderHelper.cs /// <summary> /// 文件夹操作类 /// ...
- python 关于文件夹的操作
在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir() # path 表示文 ...
随机推荐
- 存储系列之 Linux ext2 概述
引言:学习经典永不过时. 我们之前介绍过存储介质主要是磁盘,先介绍过物理的,后又介绍了虚拟的.保存在磁盘上的信息一般采用文件(file)为单位,磁盘上的文件必须是持久的,同时文件是通过操作系统管理的, ...
- search(16)- elastic4s-内嵌文件:nested and join
从SQL领域来的用户,对于ES的文件关系维护方式会感到很不习惯.毕竟,ES是分布式数据库只能高效处理独个扁平类型文件,无法支持关系式数据库那样的文件拼接.但是,任何数据库应用都无法避免树型文件关系,因 ...
- 树点分治入门题poj1741
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24253 Accepted: 8060 Description ...
- OpenStack之Neutron模块
一:简介 一.概述 1. 传统的网络管理方式很大程度上依赖于管理员手工配置和维护各种网络硬件设备:而云环境下的网络已经变得非常复杂,特别是在多租户场景里,用户随时都可能需要创建.修改和删除网络 ...
- [SD心灵鸡汤]003.每月一则 - 2015.07
乔布斯去世了,但他留给世人的财富却很多,值得每个人学习.他是个精力充沛魅力无限的家伙,同时也是一个很会鼓动人心的激励大师,甚至在他的平常对话中,经典的语句也常常脱口而出. 这里摘取了一些他的经典语录, ...
- CNN卷积神经网络的卷积层、池化层的输出维度计算公式
卷积层Conv的输入:高为h.宽为w,卷积核的长宽均为kernel,填充为pad,步长为Stride(长宽可不同,分别计算即可),则卷积层的输出维度为: 其中上开下闭开中括号表示向下取整. MaxPo ...
- VNC下载,Windows系统下VNC如何下载和安装!
IIS7服务器管理工具是一款VNC viewer的客户端,能够操作VNC,进行日常的功能实现:同时还可进行FTP的日常操作,能够高效地利用FTP的传输功能:也可以批量操作Windows.Linux系统 ...
- OAuth + Security - 3 - JWT令牌
为什么使用JWT令牌 在上面的资源服务器中,通过配置,我们了解到,当我们拿着token去获取资源时,程序会先去调用远程认证服务器的端点去验证解析token,或者在本地解析校验token,这样毫无疑问, ...
- jchdl - RTL实例 - And2(结构体的使用)
https://mp.weixin.qq.com/s/qTgeBF9N0mx5UK3xWDb3jg jchdl对Verilog做了增强,增加了用户自定义结构体类型.使用自定义结构体,可以对输入和输 ...
- 我终于搞清了啥是 HTTPS 了
引言 最近上海连续下了一周雨,温度一夜之间回到解放前,穿夏装的我被冻得瑟瑟发抖,躲在家里哪也不想去. 在家百无聊赖的刷着网页,看到公众号后台的留言,有同学问我 HTTP 和 HTTPS 有啥区别? 这 ...