zoukankan      html  css  js  c++  java
  • 文件夹下某类文件删除,文件夹下文件名读取

    from os import  listdir
    import os
    #为什么这样导入都起作用??因为__init__.py作了导入
    import random
    # a = listdir('E:smokingsmokingdataimg')
    
    
    
    # with open("E:/smoking/smoking/data/trainval.txt",'a') as s:
    #     for element in a:
    #         if element.endswith(".jpg"):
    #             s.write("data/img/" + element + "
    ")
    
    
    
    
    
    # with open("E:/smoking/smoking/data/trainval.txt", 'r') as s:
    #     raw_list = s.readlines()
    #     random.shuffle(raw_list)
    # with open("E:/smoking/smoking/data/valid.txt", 'w') as fw:
    #     for i in range(200):                           #随机抽取数目 n
    #         fw.writelines(raw_list[i])
    # #
    #
    # with open("./data/test.txt", 'r') as s:
    #     raw_list = s.readlines()
    #     random.shuffle(raw_list)
    # with open("./data/valid.txt", 'w') as fw:
    #     for i in range(150):                           #随机抽取数目 n
    #         fw.writelines(raw_list[i])
    
    # a = listdir("./data/test.txt")
    # b = listdir("./data/valid.txt")
    
    
    
    with open("E:/smoking/smoking/data/trainval.txt", 'r') as trainval:
        raw_list1 = trainval.readlines()
    
    
    with open("E:/smoking/smoking/data/valid.txt", 'r') as valid:
        raw_list2 = valid.readlines()
    
    with open("E:/smoking/smoking/data/train.txt", 'w') as s:
        for ele in raw_list1:
                if ele in raw_list2:
                    pass
                else:
    #               #随机抽取数目 n
                    s.writelines(ele)
    
    
    
        # for i in a:
        #     if i in b:
        #         pass
        #     else:
        #         s.write(i)
    
    # 删除文件夹下某种类型文件
    # a = listdir('E:smokingsmokingdataimg')
    # for e in a:
    #     if e.endswith(".txt"):
    #         os.remove('E:\smoking\smoking\data\img\'+ e)
    
  • 相关阅读:
    DOM总结
    BOM总结
    备忘录设计模式
    策略模式
    迭代器模式
    观察者模式
    装饰模式
    脚本工具(获取某个文件夹下的所有图片属性批量生成css样式)
    ajax传输中文乱码解决方法
    java Serialization and Deserializaton
  • 原文地址:https://www.cnblogs.com/Henry-ZHAO/p/13689542.html
Copyright © 2011-2022 走看看