python ABC
因为项目需要,总是会有各种各样要重命名文件的场合,manual的方法当然不可取,bat的方法又感觉不够强大,所以就从零开始学python,就为了能够自动批量修改文件名,倒腾了一个周六,总算可以了 :)
效果呢?就是把文件从
XXXXX0.bin
XXXXX2.bin
XXXXX4.bin
XXXXX5.bin
.....
转化成了
0.bin
1.bin
2.bin
.....
方便了程序直接读取这些Bin文件。
脚本如下:
import os path = "C://Users//ll//Desktop//pic" # 2016/5/21 # rename files in one subfile #############remove the unnecessary name############# for file in os.listdir(path): #filename = file.split('.')[0] #filenum = filename.split('_')[6] #num = filenum.split('D')[1] #print(file) #print(num + '.bin') #os.rename(path + file, num + '.bin') filename = file[58:] originname = 'C://Users//ll//Desktop//pic' + '//' + file; newname = 'C://Users//ll//Desktop//pic' + '//' + filename; print(originname); print(newname); os.rename(originname,newname); ####################################################### file = os.listdir(path) print (file) ###########sort####################################### for i in range(len(file)): file[i] = file[i].split('.') file[i][0] = int (file[i][0]) print('>>>>>>>>') file.sort() print(file) ###########rename##################################### for i in range(len(file)): print(file[i]) file[i][0] = str(file[i][0]) file[i] = file[i][0] + '.' + file[i][1] newfile = str(i) originname = 'C://Users//ll//Desktop//pic' + '//' + file[i] newname = 'C://Users//ll//Desktop//pic' + '//' + newfile + '.bin'; print(originname) print('new name to be:') print(newname) os.rename(originname,newname) print(file[i]) print('\n') print('successfully rename file!!! Great Job!!!') input()