对某个文件夹中的文件重命名的时候,发现有些文件丢失,代码如下:
#coding=gbk # Findthe every dir, if 01.rm exist in it, then rename it. #!/usr/bin/python ''''' Utilitiesof file & directories. ''' import os import reimport stringimport time # Get theall files & directories in the specified directory (path). def get_recursive_file_list(path): current_files = os.listdir(path) all_files = [] for file_name in current_files: full_file_name = os.path.join(path,file_name) all_files.append(full_file_name) if os.path.isdir(full_file_name): next_level_files =get_recursive_file_list(full_file_name) all_files.extend(next_level_files) return all_files def need_rename(filename): if not os.path.isfile(filename): return False base_filename = os.path.basename(filename) pattern = re.compile("^[\d]{1,3}.jpeg$") if pattern.match(base_filename): #print base_filename return True else: print "no" return False def rename(filename): old_filename = filename base_filename = os.path.basename(filename) dir_name = os.path.dirname(filename) #print "%s ----------- %s" % (dir_name, base_filename) #story_name = os.path.basename(dir_name) #print "story name: ", story_name #new_filename = os.path.join(dir_name, story_name[3:] + " " + base_filename) AllName = filename.split('/')[-1] digit = AllName.split('.')[0] newDigit = string.atoi(digit)-4 new_filename = dir_name + "/" +str(newDigit)+".jpeg" #os.rename(old_filename, new_filename) #os.system("mv "+old_filename +" "+ new_filename) time.sleep(3) print "%s ==> %s" % (old_filename, new_filename) def numeric_compare(x, y): xAllName = x.split('/')[-1] xdigit = xAllName.split('.')[0] yAllName = y.split('/')[-1] ydigit = yAllName.split('.')[0] return string.atoi(xdigit) - string.atoi(ydigit) top_dir = "/mnt/tishare/web/pdf/1125" all_files = get_recursive_file_list(top_dir)flag=Trueprint(sorted(all_files))#print(sorted(all_files, cmp=numeric_compare))for file in all_files: if flag: os.chdir(top_dir) flag=False if not need_rename(file): continue rename(file)
出错的代码标红,打印出发现其列表为:
['/mnt/tishare/web/pdf/1125/10.jpeg', '/mnt/tishare/web/pdf/1125/11.jpeg', '/mnt/tishare/web/pdf/1125/12.jpeg', '/mnt/tishare/web/pdf/1125/13.jpeg', '/mnt/tishare/web/pdf/1125/14.jpeg', '/mnt/tishare/web/pdf/1125/4.jpeg', '/mnt/tishare/web/pdf/1125/5.jpeg', '/mnt/tishare/web/pdf/1125/6.jpeg', '/mnt/tishare/web/pdf/1125/7.jpeg', '/mnt/tishare/web/pdf/1125/8.jpeg', '/mnt/tishare/web/pdf/1125/9.jpeg']
也就是说其排序有问题,先输出的/mnt/tishare/web/pdf/1125/10.jpeg,其修改为/mnt/tishare/web/pdf/1125/6.jpeg,发现其已经存在,然后造成/mnt/tishare/web/pdf/1125/6.jpeg文件丢失。自定义排序函数,在python2.x中cmp参数指定的函数用来进行元素间的比较。我们在其上自定义numeric_compare排序函数,并使用之,然后就不会出现问题了:
['/mnt/tishare/web/pdf/1125/4.jpeg', '/mnt/tishare/web/pdf/1125/5.jpeg', '/mnt/tishare/web/pdf/1125/6.jpeg', '/mnt/tishare/web/pdf/1125/7.jpeg', '/mnt/tishare/web/pdf/1125/8.jpeg', '/mnt/tishare/web/pdf/1125/9.jpeg','/mnt/tishare/web/pdf/1125/10.jpeg', '/mnt/tishare/web/pdf/1125/11.jpeg', '/mnt/tishare/web/pdf/1125/12.jpeg', '/mnt/tishare/web/pdf/1125/13.jpeg', '/mnt/tishare/web/pdf/1125/14.jpeg']
然后重命名文件就没有问题了。
参考:
http://www.jb51.net/article/57678.htm
http://blog.csdn.net/a_flying_bird/article/details/24116907