Next / Previous / Contents / Shipman's homepage

10. writeTreeFile(): Write the flat tree file

nomcompile3
# - - -   w r i t e T r e e F i l e

def writeTreeFile ( txny ):
    '''Write the flat file of taxa from txny.

      [ txny is a Txny instance ->
          if file tree-file(Args()) can be opened new for writing ->
            that file  :=  flat tree file representing txny
          else ->
            Log()  +:=  error message(s)
            stop execution ]
    '''
    #-- 1 --
    # [ if Args().treeFileName can be opened new for writing ->
    #     treeFile  :=  that file, so opened
    #   else ->
    #     Log()  +:=  error message(s)
    #     stop execution ]
    try:
        treeFile = file ( Args().treeFileName, 'w' )
    except IOError, detail:
        Log().fatal ( "Can't open the tree file '%s' for writing: "
                      "%s." % (Args().treeFileName, detail) )

We write one line to the file for each taxon in the tree. The lines are generated in preorder traversal, that is, with the root node first, followed by its first child, followed by its first child's first child, and so forth. For the recursive function that walks the tree, see Section 11, “writeSubtree(): Recursive tree walker”.

nomcompile3
    #-- 2 --
    # [ treeFile  +:=  flat taxon records from txny.taxaTree, in
    #                  pre-order ]
    writeSubtree ( treeFile, txny.taxaTree.root )
    treeFile.close()