I'm pretty sure this is a caching problem.
f = open("test.txt","w")
opens the text file for writing and sends the data
abc to it, but this data is not immediately written to disk. It remains in a cache in RAM until
- More data is sent to the file, so the cache gets full and is flushed to disk
- The code explicitly forces to write the data to disk by
- The code closes the file by
- The code reaches the end of the scope where
f is valid - for example end of a function or end of the python session
If the two lines of code are just typed into a python terminal like IDLE, the data remains in the cache.
If the two lines of code are stored in a file, and this file is loaded into a python terminal, this is like typing the code - the data remains in cache.
If the two lines of code are stored in a file, and this file is executed by double click, running
or similar, then python starts, executes the code, and then quits. When python quits, it safely deletes all objects in its memory, which also means it closes all open files. At that moment, the data appears in the file.