Question
How to sleep for some time and read a closed file and print it?
Answer
I guess what you wish to do is the following.
You already have another program or function that reads the CPU temperature, probably from the CPU FAN pin output, perhaps once every second, and write or
append the temperature readings in a text file.
You don't want to read and print the temperature readings too often.
So you only print the readings one by one, between sleeping for 5 seconds, say.
Now let me suggest to eat the big elephant by two bites:
Bite 1 - How to open/write/read/print a text file
You might like to read my program doing that in the following post. There is a minimal, complete, and verifiable program with sample output. You just plug and play. Check the results output to make sure everything goes smoothly.
How to open/write/read/print a text file? - tlfong01
Bite 2 - How to open an old file, read something, and print it with delay.
Now I have changed the above program a little bit and add sleeps between printing something. An sample output is listed below:
Now opening text file
Now writing a line to text file
Now closing text file
Now sleeping 4 seconds, ...
Now printing text file
Below is a complete listing of the minimal, complete, verifiable program. Again you just plug and play. I am using Chinese Windows, so you might see funny characters in the print output. In that case, you might try my PenZu backup file below.
tlfong01's PenZu delay print text file program listing
1:07PM SAT. 4/20/2019
# *** write_test_file_test06 tlfong01 2019apr20hkt1257 ***
import datetime
from time import sleep
# *** Text file functions ***
# *** Open/Close Text File ***
def openTextFileToWrite(textFileName):
textFileObject = open(textFileName, 'w')
return textFileObject
def openTextFileToRead(textFileName):
textFileObject = open(textFileName, 'r')
return textFileObject
def closeTextFile(textFileObject):
textFileObject.close()
return
# *** Write/Read/Print Text File ***
def writeTextString(textFileObject, writeString):
textFileObject.write(writeString)
return
def printTextFile(textFileName):
textFileObject = open(textFileName, 'r')
for line in textFileObject:
print(line)
textFileObject.close()
return
# *** Test Functions ***
def writeTimeNowString(textFileObject):
textFileObject.write('\r\n' + str(datetime.datetime.now()))
return
def testWriteReadPrintTextFile(textFileName):
textFileObject = openTextFileToWrite(textFileName)
writeTimeNowString(textFileObject)
closeTextFile(textFileObject)
printTextFile(textFileName)
return
def testWriteReadPrintSleepTextFile(textFileName):
print('')
print(' Now opening text file ', textFileName, '\r\n')
textFileObject = openTextFileToWrite(textFileName)
print(' Now writing a dateTime line to text file', textFileName, '\r\n')
writeTimeNowString(textFileObject)
writeTimeNowString(textFileObject)
print(' Now closing text file ', textFileName, '\r\n')
closeTextFile(textFileObject)
print(' Now sleeping 4 seconds, ...', '\r\n')
sleep(4)
print(' Now printing text file ', textFileName, '\r\n')
printTextFile(textFileName)
return
# *** Main ***
print('Begin testWriteReadPrintTextFile, ...')
#testWriteReadPrintTextFile('testFile01.txt')
testWriteReadPrintSleepTextFile('tom.txt')
print('\r\nEnd testWriteReadPrintTextFile.')
# *** Sample Output ***
'''
>>>
RESTART: /home/pi/Python Programs/Python_Programs/test1197/test_text_file_05_2019apr2001.py
Begin testWriteReadPrintTextFile, ...
Now opening text file tom.txt
Now writing a dateTime line to text file tom.txt
Now closing text file tom.txt
Now sleeping 4 seconds, ...
Now printing text file tom.txt
2019-04-20 12:59:18.491911
2019-04-20 12:59:18.492096
End testWriteReadPrintTextFile. testWriteReadPrintTextFile.
>>>
'''
# *** End ***
1:07PM SAT. 4/20/2019
Update 2019apr22hkt1112
[Introduction to Computer Science and Programming in Python {2.7?] - MIT OCW]3
Introduction to Computer Science and Programming Using Python [3.5?]
sleep(30)
work or not? The comment in your code is a little confusing ("...but it did do the trick.") so I'm wondering if it's a typo. – Roger Jones Apr 19 '19 at 21:40