由于给客户的发布版本上客户改动了些代码和图片,我们这边给他们更新publish都是增量更新(开发提供更新指定的文件,我们提取出来给客户进行覆盖更新),但有时需要更新的文件较多导致不得不一个一个的进行查找、替换,工作量大而且容易出错。所以用python写个保留pulish后目录的指定文件、删除其他文件的功能。
代码如下:
import os import os.path def DeleteFiles(path,fileList): for parent,dirnames,filenames in os.walk(path): FullPathList = [] DestPathList = [] for x in fileList: DestPath = path + x DestPathList.append(DestPath) for filename in filenames: FullPath = os.path.join(parent,filename) FullPathList.append(FullPath) for xlist in FullPathList: if xlist not in DestPathList: os.remove(xlist)
for x in fileList: DestPath = path + x DestPathList.append(DestPath)
for filename in filenames: FullPath = os.path.join(parent,filename) FullPathList.append(FullPath)
for xlist in FullPathList: if xlist not in DestPathList: os.remove(xlist)