def file_name_batch_modify(path=None, searchContents=None, replacedContents=None, fileFormat=None):
if fileFormat == None:
return -1
if not os.path.exists(path) or not os.path.isdir(path):
return -2
if not (isinstance(searchContents,(str,list)) and isinstance(replacedContents,(str,list))):
return -3
else:
if type(searchContents) != type(replacedContents):
if isinstance(searchContents,str):
searchContents = [searchContents]
else:
replacedContents = [replacedContents]
if len(searchContents)!=len(replacedContents):
for i in range(max(len(searchContents), len(replacedContents))):
if len(searchContents) < len(replacedContents):
searchContents.append(searchContents[0])
else:
replacedContents.append(replacedContents[0])
if isinstance(fileFormat, str):
fileFormat = [fileFormat]
elif not isinstance(fileFormat,list):
return -4
listDir = os.listdir(path)
try:
for i in range(len(listDir)):
fileName = listDir[i]
if not os.path.isfile(os.path.join(path, fileName)):
continue
suffix = fileName.split('.')[-1]
if fileFormat.count(suffix)==0:
continue
tempName = fileName
for _ in searchContents:
if fileName.__contains__(_):
tempName = tempName.replace(_,replacedContents[searchContents.index(_)])
os.rename(os.path.join(path, fileName),
os.path.join(path, tempName))
return 0
except:
return -999