1. 程式人生 > >【python】重新命名移動圖片,rename file 不管是什麼格式的檔案

【python】重新命名移動圖片,rename file 不管是什麼格式的檔案

'''
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)
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