A colleague and I were reviewing some student submissions. He likes using IDLE, while I use PyCharm. The student developed their code in PyCharm. A simplified example of the students work is:
file = open('test_file.txt','w')
file.write('This is a test file.')
print('Completed')
exit()
The student has made an error in their file handling and created a situation where the file is left open after the code is completed.
When run from within PyCharm the file.write is completed and the file is updated as expected. When run from within IDLE the file.write appears not to be completed and the file is empty. When run from the command line (btw, we all use macbooks) the file.write is completed and the file has the line of text. One more clue is that when run within IDLE after 'Completed' is output, there is a system dialog that states, "Your program is still running! Do you want to kill it?". This warning does not appear when run from the command line or within PyCharm.
We are trying to understand the difference between these behaviours given that we believe they are all doing the same process of invoking the same interpreter.
IDLE leaves the interpreter running after executing the code. You can go back to the Shell and inspect variables, for example. If you exit idle or restart the shell (Ctrl-F6) the interpreter exits (or restarts) and the file will be flushed and closed. Without restarting the shell, the file will still be open and cached writes may not have been written to disk yet.
The other IDEs appear to exit the interpreter when the script completes. It's a design decision by the developers.
You would get a similar result from the command line by running python -i script.py
, which leaves the interpreter running after executing script.py
. The file won't be flushed until exiting.
FYI: The exit()
is causing the IDLE system message. It kills the interpreter and closes the Shell, but since it is a kill
and not a clean exit the file isn't flushed.