Skip to content

Instantly share code, notes, and snippets.

@sgobin
Created February 15, 2018 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgobin/82fbcf6a83622963579e52cdd1dc58e6 to your computer and use it in GitHub Desktop.
Save sgobin/82fbcf6a83622963579e52cdd1dc58e6 to your computer and use it in GitHub Desktop.
Check for imagens not used or referenced in HTML or CSS files in a site project
# Recursively check for images in a directory and compare with referenced images in CSS and HTML files
# in tha folder, generating two CSV files:
# One with used images and one with not referenced ones
# (does not look into JS files)
import glob
import csv
# Create the list of file names
pasta = "src/" #root of the project source files
pastaIMG = "src/img/" #root of images folder
buscaPNG = "**/*.png"
buscaJPG = "**/*.jpg"
buscaGIF = "**/*.gif"
pngs = glob.glob(pastaIMG + buscaPNG, recursive = True)
jpgs = glob.glob(pastaIMG + buscaJPG, recursive = True)
gifs = glob.glob(pastaIMG + buscaGIF, recursive = True)
htmls = glob.glob(pasta + "**/*.html", recursive = True)
csss = glob.glob(pasta + "**/*.css", recursive = True)
imagens = pngs + jpgs + gifs # join all image lists into one
imagens[:] = [i[4:] for i in imagens] # remove o /src from the path, must be changed for other names
fontes = htmls + csss
# Search files looking for references for the image
imgsUsadas = []
for arquivo in fontes:
fonte = open(arquivo, "r")
for l in fonte:
for img in imagens:
if img in l:
imgsUsadas.append(img)
imagens.remove(img)
# Write lists to csv files
arqImgsUsadas = "site-used-images.csv"
arqImgsNaoUsadas = "site-images-NOT-referenced.csv"
with open(arqImgsUsadas, "w") as output:
writer = csv.writer(output)
for val in imgsUsadas:
writer.writerow([val])
with open(arqImgsNaoUsadas, "w") as output:
writer = csv.writer(output)
for val in imagens:
writer.writerow([val])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment