Exporting to columns of text

The following Python script exports several variables from a multi-domain database into several text format Xmdv files which are then combined into a single text file containing the columns of data from all domains. The domains are appended one after the other.

def ExportVariablesToTextFile(output, input, vars):
    import os
    DeleteAllPlots()
    OpenDatabase(input)
    AddPlot("Pseudocolor", vars[0])
    DrawPlots()

    # Export the database as Xmdv (a column text format)
    dbAtts = ExportDBAttributes()
    dbAtts.db_type = "Xmdv"
    dbAtts.filename = "exported"
    if len(vars) > 1:
        dbAtts.variables = tuple(["default"] + list(vars[1:]))
    else:
        dbAtts.variables = ["default"]
    opts = GetExportOptions("Xmdv")
    opts["Export coordinates?"] = 0
    ExportDatabase(dbAtts, opts)
    os.system("sleep 3")

    # Figure out which okc files were created
    files = os.listdir(".")
    doms = []
    for f in files:
        if f[:8] == "exported" and f[-4:] == ".okc":
            doms = doms + [f]
    doms.sort()
    print doms

    # Iterate over the domain files and append them together,
    # skipping the small header at the top of the okc files.
    f = open(output, "wt")
    for dom in doms:
        inp = open(dom, "rt")
        lines = inp.readlines()
        inp.close()
        for i in range(2*len(vars)+1, len(lines)):
            f.write(lines[i])
    f.close()

# Try it out
db = "~/data/multi_rect2d.silo"
vars_to_export = ("u", "v")
ExportVariablesToTextFile("combined.txt", db, vars_to_export)