classification
Title: Operation conflict between time package and file
Type: compile error Stage: resolved
Components: IO Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: minaki_2525, terry.reedy
Priority: normal Keywords:

Created on 2021-03-02 10:43 by minaki_2525, last changed 2021-03-05 22:56 by terry.reedy. This issue is now closed.

Files
File name Uploaded Description Edit
test.py minaki_2525, 2021-03-02 10:43 The following code can be run after commenting sleep, otherwise the file will always be empty
Messages (2)
msg387909 - (view) Author: Minaki (minaki_2525) Date: 2021-03-02 10:51
"""In Python 3.9, the following code can be run after commenting sleep, otherwise the file will always be empty"""
import time

file=open("data.txt","a+")
while True:
    time.sleep(0.00001)
    file.write("test\n")
    print("add over")
msg388171 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-03-05 22:56
I unlinked the preliminary versions of the code minimized in the 3 post.

The posted code is buggy: it opens a file and then start an infinite loop.  It has to be interrupted someway, and the file may not be properly closed and flushed to disk.  When I make the loop finite and explicitly close the file, or even better, use a 'with' statement, the file is written as expected.  At least on Windows, it runs noticeably slower with the sleep because the minimum sleep is 1/16 second.

Minaki, if you find a problem with unbuggy code, you can post and reopen.  If you are a beginner who does not know about things like closing file, flushing to disk, and the with statement, you should post to a question and answer forum such as python-list and ask 'Is Python or my code buggy?'
History
Date User Action Args
2021-03-05 22:56:17terry.reedysetstatus: open -> closed


title: Operation conflict between time package and file in python 3.8 3.9 -> Operation conflict between time package and file
nosy: + terry.reedy
versions: + Python 3.10, - Python 3.8, Python 3.9
messages: + msg388171
resolution: not a bug
stage: resolved
2021-03-05 22:43:29terry.reedysetmessages: - msg387908
2021-03-05 22:43:20terry.reedysetmessages: - msg387907
2021-03-02 10:51:05minaki_2525setmessages: + msg387909
2021-03-02 10:49:23minaki_2525setmessages: + msg387908
2021-03-02 10:43:49minaki_2525create