批量取出文件夹中图片的名字存进文本,并去命名新的文件夹下的图片

这个任务为具体分为两步完成的:

1、取出文件中的图片的名字存入到文本中;

2、逐行取出文本中的名字去逐个命名新文件夹下的图片;

任务一:

 1 # -*- coding:utf-8 -*-
 2 import sys
 3 sys.path.append('D:\tensorflow\install\libs')
 4 import os #os:操作系统相关的信息模块
 5 import random #导入随机函数
 6 #存放原始图片地址
 7 #data_base_dir = r"F:\underwater slam\VOC\VOC2007\JPEGImages"
 8 data_base_dir = r"F:\1picture"
 9 file_list = [] #建立列表,用于保存图片信息
10 #读取图片文件,并将图片地址、图片名和标签写到txt文件中
11 write_file_name = r'F:\dir.txt'
12 write_file = open(write_file_name, "w") #以只写方式打开write_file_name文件
13 for file in os.listdir(data_base_dir): #file为current_dir当前目录下图片名
14     if file.endswith(".jpg"): #如果file以jpg结尾
15         write_name = file #图片路径 + 图片名 + 标签
16         file_list.append(write_name) #将write_name添加到file_list列表最后
17 sorted(file_list) #将列表中所有元素随机排列
18 number_of_lines = len(file_list) #列表中元素个数
19 #将图片信息写入txt文件中,逐行写入
20 for current_line in range(number_of_lines): 
21     write_file.write(file_list[current_line] + '\n')
22 #关闭文件
23 write_file.close()

任务二:

 1 #include <iostream>  
 2 #include <io.h>  //对系统文件进行操作的头文件
 3 #include <string>  
 4 #include <sstream>
 5 #include<vector>
 6 #include <fstream>
 7 
 8 using namespace std;
 9 
10 const string FileType = ".jpg";    // 需要查找的文件类型
11 
12 int main()
13 {
14     _finddata_t c_file;   // 查找文件的类
15 
16     string File_Directory = "F:\\1picture";   //文件夹目录
17     string strFlie = "F:\\draft\\draft\\rgbd_dataset_freiburg2_desk\\dir.txt";
18     ifstream f;
19 
20     f.open(strFlie.c_str());
21 
22     string buffer = File_Directory + "\\*" + FileType;
23 
24     //long hFile;  //win7系统,_findnext()返回类型可以是long型
25     intptr_t hFile;   //win10系统 ,_findnext()返回类型为intptr_t ,不能是long型
26     hFile = _findfirst(buffer.c_str(), &c_file);   //找第一个文件
27 
28     if (hFile == -1L)   // 检查文件夹目录下存在需要查找的文件
29         printf("No %s files in current directory!\n", FileType);
30     else
31     {
32         printf("Listing of files:\n");
33 
34         int i = 0;
35         string newfullFilePath;
36         string oldfullFilePath;
37         string str_name;
38         do
39         {
40             oldfullFilePath.clear();
41             newfullFilePath.clear();
42             str_name.clear();
43 
44             getline(f, str_name);
45             if (str_name == "")
46             {
47                 cout << "文件数大于文件名" << endl;
48                 break;
49             }
50 
51             //旧名字
52             oldfullFilePath = File_Directory + "\\" + c_file.name;
53             //新名字
54             newfullFilePath = File_Directory + "\\" + str_name + FileType;
55 
56             /*重命名函数rename(const char* _OldFileName,const char* _NewFileName)
57             第一个参数为旧文件路径,第二个参数为新文件路径*/
58             int c = rename(oldfullFilePath.c_str(), newfullFilePath.c_str());
59 
60             if (c == 0)
61                 puts("File successfully renamed");
62             else
63                 perror("Error renaming file");
64 
65         } while (_findnext(hFile, &c_file) == 0);  //如果找到下个文件的名字成功的话就返回0,否则返回-1  
66         _findclose(hFile);
67     }
68     system("pause");
69     return 0;
70 }

猜你喜欢

转载自www.cnblogs.com/wall-e2/p/9829330.html