用python删除指定文件夹下重复文件

334 阅读1分钟

今天在整理电脑文件时,发现f盘中的一个文件夹里有很多重复的rar格式的压缩文件。手动删除太费力,就搞个python程序自动> 删除一哈。

  import os   
  
  def del_files(path):
      for root, dirs, files, in os.walk(path):
          for name in files:
              if name.endswith(".rar"):
              os.remove(os.path.join(root, name))
              print("Delete file:" + os.path.join(root, name))

  if __name__ == "__main__":
      path="文件路径"
      del_files(path)