0

I use the following code to write data to a file:

fr = open('filename', 'w')
for x in lines: #lines is a list with about 10k numbers
    f.write(str(x) + ' ')

The data of last lines or the last line get lost sometimes, but I changed the buffersize to 0, which means modifying code to fr = open ('filename', 'w', 0), no data lose. Why should I use this parameter? What the use of buffersize parameter? Any other way to prevent the loss?

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
ChaosCosmos
  • 107
  • 8
  • 1
    did you close the file at the end? – JoeC Apr 12 '14 at 18:09
  • @JoeC No, I found some people saying whether closing or not has no difference here, they just suggest changing the buffersize to 0; I didn't test it. – ChaosCosmos Apr 12 '14 at 18:30
  • Well, give it a shot, just add one line after your loop, `fr.close` – JoeC Apr 12 '14 at 18:36
  • @JoeC I tested it just now, it was weird that, no data lose without parameter 0 or `fr.close()` now! so I can't tell if `fr.close()` works. – ChaosCosmos Apr 12 '14 at 19:11

1 Answers1

2

Buffer means that the data not immediately written to the file on disk, but stored in memory buffer of size x. When the buffer is full the system flushes the content to the file, that is when the actual content is written to the file on disk. One more occasion when the content is being flushed to disk is when the file is being "closed". But when you set the buffer to be of size 0(zero) - the content does not goes into memory, but straight (more or less) written to the file. That is why when you close the file at the end of the loop, or set the buffer to 0 you have all the data. But when you don't close, and the buffer is positive - your last line of data is left hanging in the memory but not written (flushed) to the disk, and you have a line missing. A good practice will be to use the python "with" keyword to open a file - with this keyword your file will be automatically closed and all the content flushed (written) to disk.

Example:

with open('filename', 'w') as fr:
     for x in lines:
         fr.write(str(x) + ' ')
Vlad Lyga
  • 1,133
  • 9
  • 10
  • 2
    the explicit `fr.flush()` call or the implicit flush on `fr.close()` (called by `with`-statement) do not write to disk directly, the calls flush only libraries buffers, OS may delay the writing to disk. It is not an issue unless there is a power failure ([`os.fsync()` may help](http://stackoverflow.com/questions/12003805/threadsafe-and-fault-tolerant-file-writes/12012813#12012813)). – jfs Apr 12 '14 at 22:23