Bulk remove the name of the picture in the folder into the text and go to name the picture under the new folder

Source: Internet
Author: User



This task is done in two steps:



1. Take out the name of the picture in the file and deposit it in the text;



2. Go through the name of the text to name the image under the new folder.



Task One:


1 # -*- coding:utf-8 -*-
 2 import sys
 3 sys.path.append(‘D:\tensorflow\install\libs‘)
 4 import os #os: operating system related information module
 5 import random #import random function
 6 #Store original image address
 7 #data_base_dir = r"F:\underwater slam\VOC\VOC2007\JPEGImages"
 8 data_base_dir = r"F:\1picture"
 9 file_list = [] #Create a list to save image information
10 #Read the image file and write the image address, image name and label to the txt file.
11 write_file_name = r‘F:\dir.txt‘
12 write_file = open(write_file_name, "w") #Open the write_file_name file in write-only mode
13 for file in os.listdir(data_base_dir): #file is the current image name in current_dir directory
14 if file.endswith(".jpg"): #iffile ends with jpg
15 write_name = file #image path + image name + tag
16 file_list.append(write_name) #Add write_name to the last file_list list
17 sorted(file_list) # randomize all the elements in the list
18 number_of_lines = len(file_list) #Number of elements in the list
19 #Write picture information to txt file, write line by line
20 for current_line in range(number_of_lines):
21 write_file.write(file_list[current_line] + ‘\n‘)
22 #Close file
23 write_file.close() 


Task Two:


1 #include <iostream>
 2 #include <io.h> //Head file for operating system files
 3 #include <string>
 4 #include <sstream>
 5 #include<vector>
 6 #include <fstream>
 7
 8 using namespace std;
 9 
10 const string FileType = ".jpg"; // File type to look up
11
12 int main()
13 {
14 _finddata_t c_file; // class to find files
15
16 string File_Directory = "F:\\1picture"; //Folder directory
17 string strFlie = "F:\\draft\\draft\\rgbd_dataset_freiburg2_desk\\dir.txt";
18 ifstream f;
19
20 f.open(strFlie.c_str());
twenty one 
22 string buffer = File_Directory + "\\*" + FileType;
twenty three 
24 //long hFile; //win7 system, _findnext() return type can be long
25 intptr_t hFile; //win10 system, _findnext() return type is intptr_t, can't be long
26 hFile = _findfirst(buffer.c_str(), &c_file); //find the first file
27
28 if (hFile == -1L) // Check for files that need to be found in the folder directory.
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 << "The number of files is greater than the file name" << endl;
48 break;
49 }
50
51 //Old name
52 oldfullFilePath = File_Directory + "\\" + c_file.name;
53 //New name
54 newfullFilePath = File_Directory + "\\" + str_name + FileType;
55
56 / * rename function rename (const char * _OldFileName, const char * _NewFileName)
57 The first parameter is the old file path, and the second parameter is the new file path*/
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); //returns 0 if the name of the next file is found successful, otherwise returns -1
66 _findclose(hFile);
67 }
68 system("pause");
69 return 0;
70 } 


Bulk remove the name of the picture in the folder into the text and go to name the picture under the new folder


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.