4

Please explain the following:

def feed(data):
  import os
  print "DATA LEN: %s" % len(data)
  f = open("copy", "w")
  f.write(data)
  f.close()
  print "FILE LEN: %s" % os.stat("copy").st_size
  t = tempfile.NamedTemporaryFile()
  t.write(data)
  print "TEMP LEN: %s" % os.stat(t.name).st_size
  t.close()

feed(x)
DATA LEN: 11004
FILE LEN: 11004
TEMP LEN: 8192

Why the difference, and can I fix temp? The end seems to be chopped.

Tested on 2.6, 2.7

Bach
  • 6,145
  • 7
  • 36
  • 61
ChristopherDBerry
  • 1,722
  • 3
  • 11
  • 19
  • The only difference I can spot in the documentation seems to be, that on Windows you have to append a "b" to open, because windows distinguishes between binary and non-binary and tempfile automatically calls "w+b". – Julius F Feb 21 '14 at 11:10
  • 3
    What happens if you `.flush()` (or `.close()` for that matter) before you `stat`? – mgilson Feb 21 '14 at 11:11
  • Bingo! Thank you. .flush() solved the problem. .close() will delete the tempfile. If you care to elucidate I'll accept. – ChristopherDBerry Feb 21 '14 at 11:15
  • FWIW, [I've seen this magic number (8192) before ...](http://stackoverflow.com/a/15983708/748858) – mgilson Feb 21 '14 at 11:20

1 Answers1

5

I think you're running into the internal write buffer size. In the first case, you .close() the file before calling os.stat which effectively flushes the internal buffer. In the second case (with the tempfile), you still have the file open when you call os.stat. Since the file is still open, some of it may still be buffered in memory until you flush it explicitly or by closing it.

mgilson
  • 300,191
  • 65
  • 633
  • 696