SHARE
TWEET

Untitled

a guest Jun 19th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import re
  2.  
  3. # detect 'IMG' in upper/lower case allowing for
  4. # zero or more spaces between the < and the 'I'
  5. img = '< *[iI][mM][gG] '
  6.  
  7. # allow any character up to the 'ALT' or 'alt' before >
  8. alt = img + '.*[aA][lL][tT].*>'
  9.  
  10. # open file and read it into list
  11. filename = raw_input('Enter a filename to search ')
  12. inf = open(filename,'r')
  13. lines = inf.readlines()
  14.  
  15. # if the line has an IMG tag and no ALT inside
  16. # add our message as an HTML comment
  17. for i in range(len(lines)):
  18.   if re.search(img,lines[i]) and not \
  19.      re.search(alt,lines[i]):
  20.         lines[i] += '<!-- PROVIDE ALT TAGS ON IMAGES! -->\n'
  21.  
  22. # Now write the altered file and tidy up.
  23. inf.close()
  24. outf = open(filename,'w')
  25. outf.writelines(lines)
  26. outf.close()
RAW Paste Data
Top