0

I was practicing opening, reading and writing to files within python3.6 directly.

I created a file called days.txt and listed the days of the week in it. Then I opened the file in write mode and confirmed from Atom it was truncated.

But when I tried to write "Sunday" to the file but it returned a character count of 6 instead of writing "Sunday" to the file.

So then I wrote the same lines of code in a script, saved it and ran it and it worked. It wrote the information to the file.

Why does it work when run as a script but not when run directly in python?

>>> fo = open("days.txt", 'w')
>>> fo.write("Sunday")
6
>>> fo.write("Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday")
62
Farmbone
  • 13
  • 3
  • Because the file object is being closed when your script exits and that causes pending buffered data to be flushed to the file. Put fo.close() in your prompt to get the same effect... (or put fo.flush() to put the data on disk and leave the file open). Data only gets written to disk once the file's buffer is exceeded and that's pretty much always going to be more than 68 bytes worth (probably 512/1024 etc...) – Jon Clements Dec 30 '17 at 21:38
  • 1
    *"Then I opened the file in write mode and confirmed from Atom it was truncated."* -- Note that opening a file in write mode deletes the file if it already exists and creates a new one with the same name -- i.e. it truncates the file. So the act of observation changes the experiment. – unutbu Dec 30 '17 at 21:47
  • Possible duplicate of [How often does python flush to a file?](https://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file) – hansaplast Dec 30 '17 at 21:53
  • @Farmbone: If you use a [`with-statement`](http://effbot.org/zone/python-with-statement.htm) to open the file, then Python will close the file for automatically upon leaving the `with-statement`. That too will flush any buffered writes to disk. – unutbu Dec 30 '17 at 22:15
  • @ubuntu "So the act of observation changes the experiment." ha, love it. – Farmbone Dec 30 '17 at 23:04
  • @JonClements I didn't understand at first. I had to read more but now it makes sense, an it works. Thanks! – Farmbone Dec 30 '17 at 23:06
  • @Farmbone : You can upvote and/or tick "accept" an answer that proved useful to you. Fair-play. I did upvoted your Q as I considered it ok (clear and useful)... and I thought it doesn't deserve to go negative. Cheers! :) – סטנלי גרונן Dec 31 '17 at 16:49

2 Answers2

0

.write() returns the number of bytes written to the file, that number can be used to check for error in case the number returned is different from the amount of bytes given, which can happen most likely in remote/online operation.

The reason why you see nothing in the file is because file operations are expensive, so the operating system delays them until the file is closed or a certain amount of bytes is to be written to the file.

That is why when you execute the script it writes (because the file was closed when exiting the script) while on console it looks like it was not (because the operation was delayed).

There are 2 ways to actually write into the file, one is closing it and the other one is doing .flush().

Copperfield
  • 8,131
  • 3
  • 23
  • 29
  • In standard English, the correct expressions are: _You see nothing_ or _You don't see anything_. In standard English, a "double negative" results in one negation cancelling out the other negation, resulting in an affirmative meaning. :) :) --> https://ell.stackexchange.com/questions/143091/i-see-nothing-vs-i-dont-see-nothing – סטנלי גרונן Dec 30 '17 at 22:11
  • @groenhen oh, ok – Copperfield Dec 30 '17 at 22:38
0

When you do fo.write("Sunday") then you tell the OS that you want to write 'Sunday' to the file. But the OS can delay that write until later.

So, if you open the file in the mean time it is truncated.

To go sure the file is "current" you can either do:

  • fo.flush()
  • stop the script (which you did) or exit the python interpreter (which you didn't). Then the file is automatically closed and all pending data is written to disk.

See this question for more details on when a write is saved to disk.

hansaplast
  • 11,007
  • 2
  • 61
  • 75