文件夹下某类文件删除,文件夹下文件名读取

from os import  listdir
import os
#为什么这样导入都起作用??因为__init__.py作了导入
import random
# a = listdir('E:\smoking\smoking\data\img')



# with open("E:/smoking/smoking/data/trainval.txt",'a') as s:
#     for element in a:
#         if element.endswith(".jpg"):
#             s.write("data/img/" + element + "\n")





# 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:\smoking\smoking\data\img')
# for e in a:
#     if e.endswith(".txt"):
#         os.remove('E:\\smoking\\smoking\\data\\img\\'+ e)
posted @ 2020-09-18 09:48  Parallax  阅读(105)  评论(0编辑  收藏  举报