Why does tempfile.NamedTemporaryFile() truncate my data?
<p>Here is a test I created to recreate a problem I was having when I used
tempfile.NamedTemporaryFile(). The problem is that when I use tempfile the
data in my CSV is truncated off the end of the file.</p>

<p>When you run this test script, temp2.csv will get truncated and temp1.csv
will be the same size as the original CSV.</p>

<p>I'm using Python 2.7.1.</p>

<p>You can download the sample CSV from <a href="http://explore.data.gov/Energy-and-Utilities/Residential-Energy-Consumption-Survey-RECS-Files-A/eypy-jxs2" rel="nofollow">http://explore.data.gov/Energy-and-Utilities/Residential-Energy-Consumption-Survey-RECS-Files-A/eypy-jxs2</a></p>

<pre><code>#!/usr/bin/env python

import tempfile
import shutil


def main():
    f = open('RECS05alldata.csv')
    data = f.read()
    f.close()

    f = open('temp1.csv', 'w+b')
    f.write(data)
    f.close()

    temp = tempfile.NamedTemporaryFile()
    temp.write(data)
    shutil.copy(temp.name, 'temp2.csv')
    temp.close()

if __name__ == '__main__':
    main()
</code></pre>
