美文网首页
将许许多多文件夹中文件名后面为_mask.png的,整理入一个文

将许许多多文件夹中文件名后面为_mask.png的,整理入一个文

作者: 晨颜 | 来源:发表于2023-03-09 20:47 被阅读0次
import os
import re
import pickle
# res=os.curdir
path1=os.path.abspath('JSON2png')# path1='D:\learn&work\Python\JSON2png'
print(path1)
os.makedirs(r'JSON2png\new_phone_directory')
# 列出JSON2png目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
directory_list=os.listdir(path1)
# print('directory_list=',directory_list)
for directory in directory_list:
    path2=path1+'\\'+directory
    # print('path2=',path2)
    file_list = os.listdir(path2)
    for file in file_list:
        # print(file)
        maskpng=re.findall('IMG.*_mask.png',file)
        if maskpng==[]:
            pass
        else:
            # print('maskpng=', maskpng)
            path_maskpng=path2+'\\'+maskpng[0]
            new_path_maskpng=path1+'\\'+'new_phone_directory'+'\\'+maskpng[0]
            print(path_maskpng)
            print(new_path_maskpng)
            with open(path_maskpng,'rb') as f1:
                with open(new_path_maskpng,'wb') as f2:
                    f2.write(f1.read())
            # 删除原图片
            os.remove(path_maskpng)# 自行决定是否加这一句代码

相关文章

网友评论

      本文标题:将许许多多文件夹中文件名后面为_mask.png的,整理入一个文

      本文链接:https://www.haomeiwen.com/subject/ynhzldtx.html