'''
copy and rename file No matter what format it is.
'''
import os
import shutil
root_path = "/media/d_2/everyday/0913/tmp/"
save_path = "/media/d_2/everyday/0913/save/";
pre_name = "HBD_JieLQianM_"
file_list = os.listdir(root_path)
file_list.sort()
cnt = 0
str_postfix = ""
for val in file_list:
#print val
src_path = os.path.join(root_path,val)
pos = val.rfind(".")
if(-1 != pos):
str_postfix = val[pos:]
else:
str_postfix=""
cnt += 1
print "cnt::%d,(%s)"%(cnt,val)
new_name = pre_name + str(cnt)+str_postfix
save_file = save_path + new_name
if(os.path.exists(src_path)):
shutil.copy2(src_path,save_file)
else:
print "%s is not exist"%src_path
加sort是因为有时候要重命名两个对应的文件夹,img和xml,不加的话会乱序