Rename the batch python script
- 2020-04-02 13:11:08
- OfStack
Will Copy of * * * * * * * *. BMP or Copy of Copy of * * * * * * * *. BMP such unified file named * * * * * * * * 0. BMP or * * * * * * * * 00 in BMP format, such as the back of the number of 0 is for * * * * * * * *. BMP the number of occurrences of + 1. Wrote the following little script:
Used the dictionary, used the OS module, learned some things, ha ha!
import os
"""
This program is used to rename file names, because there are many in the folder Copy of Duplicate file name, so remove windows
Rename the style and use the new cumulative naming mechanism instead
The author: chenxofHit@gmail.com
Time: 2011 years 4 month 13 day
"""
def getFileNames(dirs, dict):
#dir Is the directory name, lst The list of
filenames=os.listdir(dirs)
for name in filenames:
key = filenames[filenames.index(name)]
sign = "Copy Of "
judge = key.find(sign)
if (judge != -1 ):
dict[key] = 1
else: # Extract the real file name
trueKey= key[-12:] # Because the real file names are in 12 position
if trueKey not in dict: # It doesn't exist in the dictionary
dict[trueKey]=1
os.rename(dirs+str(os.sep)+name,dirs+str(os.sep)+trueKey)
else:
#split finames
newKey= str(trueKey[:-4])+str('0'*dict[trueKey])+ str(trueKey[-3:])
dict[trueKey] = dict[trueKey]+1
os.rename(dirs+os.sep+name,dirs+os.sep+newKey)
if '__name__=__main__':
dict={}
dirs = "C://temp"
getFileNames(dirs, dict)
Used the dictionary, used the OS module, learned some things, ha ha!