0

I got this simple code in Python that writes a "abc" into a text file calls test.txt. Now, on a Raspberry execute this code with IDLE or Thonny does NOT work, the text file remains empty... but instead, when call with the terminal, it writes the text into the file. Can someone help?

f = open("test.txt","w") 
f.write("abc")
j.doe
  • 662
  • 4
  • 19
ddd
  • 167
  • 1
  • 7
  • If written on one line it is a syntax error anyway. Probably you terminate the terminal before reading the file. This triggers the flushing of file buffer and closing of the file (which should better be done explicitly in the code). – Michael Butscher May 11 '19 at 03:21
  • hey, i just edited the code, of course it is written in 2 lines – ddd May 11 '19 at 03:23

4 Answers4

4

Python buffers file writes by default. It does this for performance purposes. Every time Python writes to a file (or OS-controlled IO stream, like STDOUT), it has to pause execution and hand control over to the OS. If the OS is busy doing something else, or if the disk where you want to write your data is busy doing something else, you could be waiting for a long time for a response. Instead of waiting every time you want to write something, Python writes the data to a buffer in memory, then promises to eventually write the contents of the buffer to a file once the buffer fills up (a process called "flushing" the buffer). This allows execution of your program to continue immediately.

The risk when using write buffers is that if your program crashes before the buffer is flushed to disk, you lose that data. Additionally, if one program writes to a buffer and continues execution in this fashion, there is no guarantee that a concurrently running program will see that data on the disk until the first program somehow flushes the buffer. This second scenario is what is going on in your example: IDLE is running one Python process that writes to a buffer, and you are running a second, concurrent process to check on the file while IDLE is still running. Because the buffer hasn't been flushed in IDLE, you will see nothing written. This does not happen when you run the program from the terminal because the Python process there terminates, and one of the clean-up tasks performed when a process terminates is to flush all the write buffers.

There are many ways to force a buffer flush. Python flushes write buffers automatically when files are closed, so you could try:

f = open("test.txt", "w")
f.write("abc")
f.close()

The preferred way to open files in Python is using a with statement context manager. When execution exits the with block, the variables created in the with statement are told to clean themselves up, which in the case of files means closing them. So you could try:

with open("test.txt", "w") as f:
    f.write("abc")  # after this block, f is flushed and closed

If you want to keep the file open and manually flush the write buffer, Python gives you the flush method, so you could also write:

f = open("test.txt", "w")
f.write("abc")
f.flush()  # f remains open

Finally, you can tell Python to use a buffer of a different size than the OS default by passing the buffering argument to open. A value of 0 tells Python to immediate flush every write to disk. So you could do this:

f = open("test.txt", "w", buffering=0)
f.write("abc")  # f remains open
PaSTE
  • 4,050
  • 18
  • 26
2

The best practice of writing files is using with, it will close and release your file automatically.

with open("test.txt","w") as f:
    f.write("abc")
Mahmoud Elshahat
  • 1,873
  • 10
  • 24
1

try to call f.close() at the end of your code?

Gang YIN
  • 2,509
  • 2
  • 21
  • 25
0

When you explicitly open the file by doing

f = open("test.txt","w") 
f.write("abc")

You need to make sure that you are closing the file at the end by doing f.close(), otherwise the data you have written to the file doesn't get flushed to the file, doesn't matter where you run the code from, so the code will be

f = open("test.txt","w") 
f.write("abc")
#Close the file
f.close()

Your other option will be to open the file using a context manager like with, which automatically closes the file once the context manager ends

with open("test.txt","w") as f:
    f.write("abc")
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40