import os, string
def myrename(path,old_ext,new_ext):
if path[-1]!="\\":
path=path+"\\"
ff = os.listdir(path)
fname = [x for x in ff if os.path.isfile(path+x)]
for file in fname:
f = os.path.splitext(file)
if f[1] == old_ext:
fnewname = f[0]+new_ext
os.rename(path+file,path+fnewname)
myrename(r"C:\Users\",".mp4",".mp3")
os.rename的使用中,文件名需要包含路径信息。