用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 表示文 ...
随机推荐
- layui 让弹窗始终居中于屏幕
前话:今天用 layer.confirm() 弹窗的时候,滚动到页面尾部再弹窗时,发现弹窗还显示在上面,要滚动会上面才能看到. 度娘找了一个获取滚动条位置的方法: function ScollPos ...
- Docker 入门:镜像
主要内容: 什么是镜像 下载镜像 pull 设置下载加速源 查看镜像 上传镜像 push 什么是镜像(image) 镜像是一个文件系统,提供了容器运行时需要用到的文件和参数配置.相当于平时在使用某个软 ...
- Java IO(一)概述
Java IO(一)概述 一.IO概述 (一).介绍 在Java中,所有的数据都是通过流读写的,Java提供了IO来处理设备之间的数据传输,Java程序中,对于数据的输入/输出操作 都是以“流”的方式 ...
- 动态生成Person类的对象 代码参考
#include <iostream> #include <string> using namespace std; class Person { private: strin ...
- 跟着阿里学JavaDay02——Java编程起步
几乎所有语言的第一个程序都是"HelloWorld" 就像所有单片机初学者一样,点亮第一个LED灯开始 而起初我们编写/学习Java程序,都是通过记事本来编写的,这里推荐一个Edi ...
- N3飞控踩坑指南
1.想要使用上位机仿真的话,在本次连接上位机的过程中不要点击IMU校准. 2.两路12S电池并联为飞控供电时(DJI智能电池),需要确保所有电池均为满电.否则如果上电时电量不平衡,电池之间将会自动互相 ...
- UML ——区分类图中的几种关系.md
目录 关联关系 (association): 聚合关系 (aggregation): 合成关系 (composition): 依赖关系 (dependency): 总结: 原文地址 http://ww ...
- Java实现 蓝桥杯 算法提高 GPA(暴力)
试题 算法提高 GPA 问题描述 输入A,B两人的学分获取情况,输出两人GPA之差. 输入格式 输入的第一行包含一个整数n表示A的课程数,以下n行每行Si,Ci分别表示第i个课程的学分与A的表现. G ...
- Java实现 LeetCode 593 有效的正方形(判断正方形)
593. 有效的正方形 给定二维空间中四点的坐标,返回四点是否可以构造一个正方形. 一个点的坐标(x,y)由一个有两个整数的整数数组表示. 示例: 输入: p1 = [0,0], p2 = [1,1] ...
- Java实现 蓝桥杯 算法提高 上帝造题五分钟
算法提高 上帝造题五分钟 时间限制:1.0s 内存限制:256.0MB 问题描述 第一分钟,上帝说:要有题.于是就有了L,Y,M,C 第二分钟,LYC说:要有向量.于是就有了长度为n写满随机整数的向量 ...