Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bloomonkey
Created July 30, 2012 11:58
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 bloomonkey/3206427 to your computer and use it in GitHub Desktop.
Save bloomonkey/3206427 to your computer and use it in GitHub Desktop.
Python (2.5 < v < 3.0) script to insert XHTML Doctype declaration at start of files where it's missing for a given directory
from __future__ import with_statement
import sys
import os
try:
myDir = sys.argv[1]
except IndexError:
myDir = os.getcwd()
for root, dirs, files in os.walk(myDir):
for name in files:
with open(os.path.join(root, name), 'r') as fileH:
page = fileH.read()
if page.startswith('<!DOCTYPE'):
# Page already has DOCTYPE
continue
with open(os.path.join(root, name), 'w') as fileH:
fileH.write('<!DOCTYPE html PUBLIC '
'"-//W3C//DTD XHTML 1.0 Strict//EN" '
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
'\n')
fileH.write(page)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment