It looks like your issue is that the csv file is open, and the arcpy function cannot work on the open/locked file.Try this:
# Import system modules
import arcpy
from arcpy import env
import os
try:
directory = "C:/temp/cert"
os.chdir(directory)
env.workspace = directory
gdb = "template.gdb"
extension = ".csv"
# get a listing of all CSV files
for myfile in os.listdir("."):
if myfile.endswith(extension):
#read in the domain values from the file
f = open(myfile, 'r')
header = f.readline().split(",")
code = header[0]
desc = header[1].rstrip().lstrip()
f.close()
del f
#Set local parameters
domTable = os.path.abspath(myfile)
codeField = code
descField = desc
dWorkspace = gdb
domName = myfile.split(extension)[0] #the domain takes on the name of the file, minus the ".csv" section
domDesc = desc.replace("_"," ")
print domTable, codeField, descField, dWorkspace, domName, domDesc
print os.path.exists(domTable)
arcpy.TableToDomain_management(domTable, codeField, descField, dWorkspace, domName, domDesc,"REPLACE")
#f.close()
except Exception, e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print "Line %i" % tb.tb_lineno
print e.message