Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
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 =
if page.startswith('<!DOCTYPE'):
# Page already has DOCTYPE
with open(os.path.join(root, name), 'w') as fileH:
fileH.write('<!DOCTYPE html PUBLIC '
'"-//W3C//DTD XHTML 1.0 Strict//EN" '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment