zoukankan      html  css  js  c++  java
  • 删掉一个文件夹相对于另一个文件夹多出的文件

    path1是一组图片,path2是标记图片对应的XML文件,

    path2和path1本应该所有文件的文件名都相同(不包括后缀),但是path2不小心混入了一些其他文件,

    现在要删掉path2下多出的文件:

     1 # coding=UTF-8
     2 import os
     3 
     4 def find(path):
     5     data = []
     6     for root, dirs, files in os.walk(path):
     7         for file in files:
     8             data.append(os.path.splitext(file)[0])
     9     return data
    10 
    11 def redundant(path, data):
    12     extra = []
    13     for root, dirs, files in os.walk(path):
    14         for file in files:
    15             name = os.path.splitext(file)[0]
    16             if name not in data:
    17                 extra.append(name)
    18     return extra
    19 
    20 def removal(path, data):
    21     for root, dirs, files in os.walk(path):
    22         for file in files:
    23             name = os.path.splitext(file)[0]
    24             if name not in data:
    25                 os.remove(root + '/' + file)
    26 
    27 path1 = 'C:/Users/Administrator/Desktop/7.20/1'
    28 path2 = 'C:/Users/Administrator/Desktop/7.20/xml1'
    29 
    30 #此目录下所有文件名(不包括后缀)放入data列表
    31 data = find('path1')
    32 #此目录相比上一个目录多出的文件名
    33 extra = redundant('path2', data)
    34 
    35 print(len(extra))
    36 #删掉多出的文件
    37 removal('path2', data)
  • 相关阅读:
    node.js
    Ajax常见面试题
    CF932E Team Work
    斯特林数
    UOJ #62. 【UR #5】怎样跑得更快
    洛谷 P4593 【[TJOI2018]教科书般的亵渎】
    洛谷 P4321 【随机漫游】
    洛谷 P4707 【重返现世】
    洛谷 P3175 [HAOI2015]按位或
    CF Gym101933K King's Colors
  • 原文地址:https://www.cnblogs.com/lxc1910/p/9342425.html
Copyright © 2011-2022 走看看