以jpg文件为例
import os
root_path = f"……"
def file_filter(file):
if os.path.splitext(file)[-1][1:] == 'jpg':
return True
return False
files = [item for item in os.listdir(root_path) if file_filter(item)==True]
count = 0
for file in files:
oldname = os.path.join(root_path, file)
newname = os.path.join(root_path, f"a{count}.jpg")
os.rename(oldname, newname)
print(oldname,'======>',newname)
count += 1