import osimport shutilroot = 'F:\\python2'for rt, dirs, files in os.walk(root): for f in files: fn = os.path.join(rt, f) nfn = fn.replace('.txt', '.py'). if src in fn and fn != nfn: shutil.move(fn, nfn)