for root,dirs,name in os.walk("."): for files in names: if files in ("1.mp4","thumb.png"): newname=os.basename(root) extname=os.path.splitext(files)[-1] os.rename(files,newname+extname)