使用py脚本查找图片是否在csd里引用

import os
import sys
import shutil
searchPath = #存放csd的文件夹
needSearchPng = ''

def get_need_search_png_Name(searchPngName):
    #print("now png name==",searchPngName)
    for pngFile in os.listdir(searchPngName):
        fileStr = searchPngName + '/'+ pngFile
        #print("fileStr===",fileStr)
        if os.path.isdir(fileStr):
            get_need_search_png_Name(fileStr)
        else:
            isFind = pngFile.find(".png")
            if isFind != -1 :
                pngName = fileStr
                get_search_files(os.path.relpath(searchPath),fileStr)

def get_search_files(searchP,pngStr):
    for file in os.listdir(searchP):
        fileStr = searchP + '/' + file
        if os.path.isdir(fileStr):
            get_search_files(fileStr,pngStr)
        else:
            check_png_is_used(fileStr,pngStr)

def check_png_is_used(file,pngName):
    #print("fileName:",file,"pngName",pngName)
    fileCsd = open(file,"w+")
    pngStrTable =  pngName.split('/')
    pngN = pngStrTable[len(pngStrTable) - 1]
    isNeedToDelete = False
    for line in fileCsd.readlines():
        isFind = line.find(pngN)
        if isFind != -1:
            isNeedToDelete = True
            #print("is can Find",pngN)
            break
    if not isNeedToDelete:
        creat_need_delete_dir(pngName)

def creat_need_delete_dir(pngPath):
    pngStrTable =  pngPath.split('/')
    endIdx = len(pngPath) - len(pngStrTable[len(pngStrTable) - 1]) - 1
    srcFilePath = pngPath[0:endIdx]

    needSTable = needSearchPng.split('/')
    lenNeedSearch = len(needSearchPng) - len(needSTable[len(needSTable) - 1]) - 1
    desFilePath = needSearchPng[0:lenNeedSearch] + os.sep + 'tempUi' + os.sep + pngPath[len(needSearchPng) + 1:endIdx]
    if not os.path.exists(desFilePath):
        os.makedirs(desFilePath)
        print("Create Dir Success!--------")
    if os.path.exists(pngPath):
        shutil.copy(pngPath,desFilePath)

if __name__ == '__main__':
    if os.path.exists(sys.argv[1]):
        global needSearchPng
        needSearchPng = sys.argv[1]
        get_need_search_png_Name(needSearchPng)

使用方法: python 这个py文件名 需要检索的png文件夹

你可能感兴趣的:(使用py脚本查找图片是否在csd里引用)