Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does tempfile.NamedTemporaryFile() truncate my data?

Tags:

python

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.

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

I'm using Python 2.7.1.

You can download the sample CSV from http://explore.data.gov/Energy-and-Utilities/Residential-Energy-Consumption-Survey-RECS-Files-A/eypy-jxs2

#!/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()
like image 668
Brent O'Connor Avatar asked Dec 10 '22 02:12

Brent O'Connor


2 Answers

Add temp.flush() after temp.write(data).

like image 172
01100110 Avatar answered Dec 27 '22 07:12

01100110


You copy the file before you close it. Files are buffered, which means that some of it will remain in the buffer while it is waiting to be written to the file. The close will write out all remaining data from the buffer to the file as part of the closing of the file.

This has nothing to do with NamedTemporaryFile.

like image 42
Mark Ransom Avatar answered Dec 27 '22 05:12

Mark Ransom