[python]将不同类别文件夹下面的图像搬到同一文件夹下
来源:互联网 发布:怎么修改淘宝域名 编辑:程序博客网 时间:2024/06/01 09:47
# -*- coding: utf-8 -*-# 用于将不同类别的文件夹下的图片搬到某个文件夹,并在文件前面加上目录名字:#比如caltech256,里面有256个文件夹,每个文件夹放的都是一类,用movefiles.py可以生成一个database的文件夹,#这个文件夹把databaseClassified的图片都搬到里面来,并且生成待查询的图片queryImgs.txt和databaseClasses.txt# python movefiles.pyimport osimport fnmatchimport shutilimport randomimport mathquery_number_percent = 0.5 # 设置每类拿百分之多少出来作为查询directory = "database" # 设置新路径databaseClasses = 'UCF-101'if not os.path.exists(directory): os.makedirs(directory)newImgDBPath = os.path.abspath(directory)# walk through the folderf = open("./databaseClasses.txt", "w")g = open("./queryImgs.txt", "w")for root, dirs, files in os.walk(databaseClasses): for i, str_each_folder in enumerate(dirs): # we get the directory path str_the_path = '/'.join([root, str_each_folder]) files_number = len((os.listdir(str_the_path))) #子目录下文件数目 # 生成查询图片实例 index = random.sample(range(0, files_number), int(math.floor(query_number_percent*files_number))) # list all the files using directory path for ind, str_each_file in enumerate(os.listdir(str_the_path)): # now add the new one str_new_name = '{0:03}'.format(i+1) +'_'+ str_each_folder + '_' + str_each_file if ind in index: g.writelines('%s\n' % str_new_name) # full path for both files str_old_name = '/'.join([str_the_path, str_each_file]) str_new_name = '/'.join([newImgDBPath, str_new_name]) # now rename using the two above strings and the full path to the files # os.rename(str_old_name, str_new_name) # 搬运原文件到设置的新目录下 shutil.copy2(str_old_name, str_new_name) # 拷贝原文件到设置的新目录下 # we can print the folder name so we know that all files in the folder are done print '%s, %d images' % (str_each_folder, files_number) f.writelines('%s %d\n' % ('{0:03}'.format(i+1)+'_'+str_each_folder, files_number))g.closef.close
0 0
- [python]将不同类别文件夹下面的图像搬到同一文件夹下
- 将不同文件夹下相同文件名的文件移动到一个文件夹下
- java文件夹操作,将多文件夹下的相同文件合成到同一文件
- Linux 把一个文件夹下的所有内容复制到另一个文件夹下面
- python读取文件夹下面的文件
- MFC工程所在的文件夹重命名后或将其搬到另一目录文件夹下时的编译错误
- 比较同一文件夹下相同大小的文件。删除相同的文件,备份到制定文件夹下
- 添加当前文件夹及其子文件夹到以及别而的文件夹到当前路径
- Python如何读取指定文件夹下的所有图像
- 把照片拷贝到同一父文件夹下
- Flume+Kafka 将不同类别日志发往不同分区的三种方式的比较
- 指定文件夹下面的所有内容copy到目标文件夹下面
- 实现一个静态方法将指定文件夹下面的所有内容copy到目标文件夹下面
- python 下载文件到文件夹下的问题
- linux 下批量解压多个文件到每个单独的文件夹下面
- android studio中res文件夹下的文件显示到java下面
- linux 下小技巧之-统计文件夹下面子文件夹下面的个数
- 遍历文件夹下的文件夹和子文件,遍历文件夹删除文件夹及其下面的所有子文件
- <global-results>
- Vanya and Exams
- 布隆过滤器
- 物资登记模块笔记
- D
- [python]将不同类别文件夹下面的图像搬到同一文件夹下
- 好玩的API调用之---三种翻译API调用
- 异步消息传递技术的比较:JMS、AMQP和MQTT
- 自动建立swap分区,开机自动激活脚本
- 连续子数组的最大和
- Python学习--配置eclipse的Python环境
- java对集合元素的特定排序
- java内部类
- 编译型语言、解释型语言、静态类型语言、动态类型语言