Python: Writing output to text file, but text file does not contain the entire string and truncates randomly?
Question: I am parsing an xls file using Python then converting that information into
[SBML](http://en.wikipedia.org/wiki/SBML) (a version of XML).

    
    
    from mod2sbml import Parser
    
    s = open('sbmltest3.mod', 'r').read()
    p = Parser()
    d = p.parse(s)
    
    outfile2 = open('sbmlconvert.xml', 'w')
    print >> outfile2, d.toSBML()
    outfile2.close()
    

This is a fairly long file (>3000 lines) and when I open the .xml, the string
is truncated randomly around 1400 or 3000 lines. However, when I type: `print
d.toSBML()` and print this string to console, the string is not truncated and
I can see the end of the parsed string.

What could be the problem here?

**Edit:** To further dissect the problem, I have closed the code with
outfile2.close() and also tried to print s and print to console in my script.
This returns both truncated `s` and `d` strings. However, when I type the
exact commands into the interpreter separately, both print correctly. Anyone
know what's going on with this discrepancy?


Answer: Try this:

    
    
    from mod2sbml import Parser    
    p = Parser()    
    
    with open('sbmlconvert.xml', 'w') as of:   
        s = open('sbmltest3.mod', 'r').read()    
        d = p.parse(s)
        of.write(d.toSBML())    
    

