第一版,能实现,但最后发现文件的顺序改变了:
import os def reename(): nm=1 pathh="/home/huangyaya/file/image/pic/chips" filelist=os.listdir(pathh) for files in filelist: Olddir=os.path.join(pathh,files) filename=os.path.splitext(files)[0] filetype=os.path.splitext(files)[1] Newdir=os.path.join(pathh,str(nm)+'.'+filetype) os.rename(Olddir,Newdir) nm+=1 reename()
新的
import os import pdb #dir_ = os.getcwd() #dir_ += '/cips' #os.chdir(dir_) path_A = "/home/huangyaya/file/image/pic/wine" path_B = "/home/huangyaya/file/image/pic/wine_output" file_number = 1 num = 0 A_list = os.listdir(path_A) B_list = os.listdir(path_B) A_list_num = 0 B_list_num = 0 for A_str in A_list: A_str_front = A_str[:-4] B_str = A_str_front + '.xml' os.rename(path_A + '/' + A_str,str(file_number) + '.jpg') os.rename(path_B + '/' + A_str_front + '.xml',str(file_number) + '.xml') file_number += 1
以上这篇python读取文件名并改名字的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。