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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.