How to take care of duplicates while copying files to a folder in python -


i writing script in python consolidate images in different folders single folder. there possibility of multiple image files same names. how handle in python? need rename "image_name_0001", "image_name_0002" this.

you can maintain dict count of names have been seen far , use os.rename() rename file new name.

for example:

dic = {} list_of_files = ["a","a","b","c","b","d","a"] f in list_of_files:     if f in dic:         dic[f] += 1         new_name = "{0}_{1:03d}".format(f,dic[f])         print new_name     else:         dic[f] = 0         print f 

output:

a a_001 b c b_001 d a_002 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -