修改文件名

来源:互联网 发布:winaip阅读器软件下载 编辑:程序博客网 时间:2024/05/16 19:28

1、分别从C++与Python语言实现文件名字修改作比较,充分体现Python语言的简介性。

2、代码主要修改指定文件夹下所有文件的文件名。

3、文件夹下的文件可以筛选,选出不想修改的文件格式,不做修改,其余的文件全部修改。

4、此处代码中只是实现了对筛选出的文件的后缀名的添加,也可以将文件名字按序增加修改,或者按照指定规律修改文件名字。

C++修改文件名:

/**改变文件后缀名1、GetAllFormatFiles(filepath, files);读出文件夹内的所有文件,2、通过对获取的所有文件进行筛选,continue方式排除带有".JPG"".xml"两种文件格式的文件3、为剩余的文件添加后缀名,此处为".xml"ps:必须是64位的调试方式*/#include #include  #include   #include#include#includeusing namespace std;//using namespace cv;/*获取特定格式的文件名该函数有两个参数,第一个为路径字符串(string类型,最好为绝对路径);第二个参数为文件夹与文件名称存储变量(vector类型,引用传递)。第三个直接使用".bmp"即可*/void GetAllFormatFiles(string path, vector& files){//文件句柄,通过对_findnexti64的查找,第一个参数是_int64型,因此写int就会报错_int64   hFile = 0;//文件信息    struct _finddatai64_t fileinfo;string p;if ((hFile = _findfirsti64(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1){do{if ((fileinfo.attrib &  _A_SUBDIR)){if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0){//files.push_back(p.assign(path).append("\\").append(fileinfo.name) );  GetAllFormatFiles(p.assign(path).append("\\").append(fileinfo.name), files);}}else{files.push_back(p.assign(fileinfo.name));  //将文件路径保存,也可以只保存文件名:    p.assign(path).append("\\").append(fileinfo.name)  }cout << "?" << endl;} while (_findnexti64(hFile, &fileinfo) == 0);_findclose(hFile);}}int main(int argc, char* argv[]){string filepath = "E:/VC_Code/数据集/输电线路标注/testXML";cout << "filepath=" << filepath << endl;vector files;string Nsfile;GetAllFormatFiles(filepath, files);char * distAll = "AllFiles.txt";ofstream ofn(distAll);int size = files.size();for (int i = 0; i < size; i++){ofn << files[i] << endl;}ofn.close();string temp, temp1, temp2;for (int i = 0; i < size; i++){temp = filepath + "/" + files[i];//带有图片位置的字符串cout << filepath << endl;cout << files[i] << endl;cout << temp << endl;char cstr[37];strcpy(cstr, temp.c_str());cout << "temp=" << temp << '\n' << "cstr=" << cstr << '\n' << "size(cstr)=" << sizeof(cstr) << endl;Nsfile = files[i].c_str();//排除".JPG"".xml"后缀文件int pos1 = Nsfile.find(".jpg");int pos2 = Nsfile.find(".xml");if (pos1 > -1 || pos2 > -1){//Nsfile.erase(pos, 4);continue;}cout << "Nsfile=" << Nsfile << endl;Nsfile = files[i] + ".xml";temp1 = filepath + "/" + Nsfile;cout << "temp1=" << temp1 << endl;if (!rename(temp.c_str(), temp1.c_str())){std::cout << "rename success " << std::endl;}else{std::cout << "rename error " << std::endl;}}system("pause");return 0;}

Python修改文件名:
import os'''#读取文件夹下指定格式文件将文件夹中的'.JPG'与'.xml'(还可以根据需要加入其他格式)文件筛选出来,不予处理,其他文件加入'.xml'(或其他后缀名)'''def getFormatFile(filepath,*unchangeFileFormat):    pathDir=os.listdir(filepath)    total_num=len(pathDir)    i=0    for item in pathDir:        x=os.path.splitext(item)[1]        print('x=',x)        if x in unchangeFileFormat:            continue        else:            src=os.path.join(os.path.abspath(filepath),item)            dst=os.path.join(os.path.abspath(filepath),item+'.xml')            print('src=',src)            print('dst=',dst)            os.rename(src,dst)            print('i=',i)            i+=1            continue    print('total %d    rename %d xml'%(total_num,i))    returnfile_path='E:/Code/Test2_Python/test_pic'unchange_file_format1='.JPG'unchange_file_format2='.xml'getFormatFile(file_path,unchange_file_format1,unchange_file_format2)


原创粉丝点击