import os import shutil sourcePath = 'D:\\python\\TangDou\\AcWing' targetPath = 'D:\\python\\TangDou' listDir = os.listdir(targetPath) # 获取当前目录下的所有内容 fmt = "\\.clang-format" vscd = "\\.vscode" for x in listDir: fName = os.path.join(targetPath, x) if os.path.isdir(fName) and fName != sourcePath: # 拷贝文件和一个子目录到目标目录下 ta = fName + fmt if os.path.exists(ta): os.remove(ta) shutil.copy(sourcePath + fmt, ta) tb = fName + vscd if os.path.exists(tb): shutil.rmtree(tb) shutil.copytree(sourcePath + vscd, tb) print("成功拷贝完成:" + fName) print("恭喜,配置文件同步到所有工作目录!")