My objective is to program a print out a reading, but I want to delay the reprint so that its not spamming me with the reading constantly.

  if(abs(cpuTemp-cpuTempOld > hyst)):
    print("Pin {0} is ON".format(FAN_PIN))
    sleep(30)   # I've tried the sleep command like this
     # but it didnt do the trick

This obviously isn't created by me and I also didn't provide the entire code, this is just want I believe you will need to have an understanding of what I'm trying to do.
  • Could you clarify, does the 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
  • Not clear what you are asking? – CoderMike Apr 19 '19 at 23:13
  • Make sure the indentation of the print() and the sleep() are the same. Sidenote, you're trying to close a file that you did not open. – Ghanima Apr 20 '19 at 0:43
  • “# but it did do the trick.” — what happened? Did it work or not?, If not what happened? – ctrl-alt-delor Apr 20 '19 at 9:22
  • didn't do# -Roger Jones I'm looking to learn why my sleep wasn't delaying the print, and how I can do that -CoderMike that is good to know I wasn't sure it the sleep needed to be 4 spaces in under what I wanted delayed or if it could be directly under and inline with the print. That was part of the code I was copying, should it be cpuFile=open.... cpuTemp=float(cpuTempFile.read())/1000 cpuFile.close() -Ghanima didn't work, it was a typo, sorry -ctrl-alt-delor thank you all for you attention and help. – Tom Paquette Apr 20 '19 at 22:25


How to sleep for some time and read a closed file and print it?


I guess what you wish to do is the following.

  1. 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.

  2. You don't want to read and print the temperature readings too often.

  3. 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):

# *** Write/Read/Print Text File ***

def writeTextString(textFileObject, writeString):

def printTextFile(textFileName):
    textFileObject = open(textFileName, 'r')    
    for line in textFileObject:

# *** Test Functions ***

def writeTimeNowString(textFileObject):
    textFileObject.write('\r\n' + str(datetime.datetime.now()))

def testWriteReadPrintTextFile(textFileName):   
    textFileObject = openTextFileToWrite(textFileName)

def testWriteReadPrintSleepTextFile(textFileName):
    print('  Now opening text file                   ', textFileName, '\r\n')
    textFileObject = openTextFileToWrite(textFileName)
    print('  Now writing a dateTime line to text file', textFileName, '\r\n')    
    print('  Now closing text file                   ', textFileName, '\r\n')      
    print('  Now sleeping 4 seconds, ...', '\r\n')
    print('  Now printing text file                  ', textFileName, '\r\n')     

# *** Main ***

print('Begin testWriteReadPrintTextFile, ...')
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?]

  • You have got my desires fr this program bang on, sorry if i didn't describe it very well. I'm definitely going to have to sit down after easter and look deeper into this, I appreciate the response and very precise instruction, I hope I'm able to obtain all of it on my own. – Tom Paquette Apr 20 '19 at 22:34
  • @Tom Paquette You are welcome. It takes time to get familiar with python's rules. I remember in the beginning I always forgot about the indentation of sleep before or after a for loop. After a while I began to appreciate indentation without the boring C/C++ brackets. Have a nice Ester holiday! :) – tlfong01 Apr 21 '19 at 2:08
  • its not an easy language to just decide to pick up with no one to refer or guide with. – Tom Paquette Apr 22 '19 at 2:35
  • @Tom Paquette Ah yes,when I first met python, I found many things confusing, including the indentation and no bracket style. I learnt python 2.7 some 5 years ago, from a free online course by MIT. I read that they now have a new joint course with edX, using python 3.5 - Introduction to Computer Science and Programming Using Python 3.5 - edx edx.org/course/… But edX courses are no longer free, if you wish to get a certificate. :) – tlfong01 Apr 22 '19 at 3:20
  • @tlfong01 a certificate isn't a bad idea, but I may just keep it as a hobby while I focus on my electrical and possible a second trade like millwright. – Tom Paquette Apr 23 '19 at 4:18

In Python you can create a delay using time.sleep(), for example to pause for 10 seconds:

import time

An alternative, exactly equivalent, way of writing this would be:

from time import sleep
  • I may be using these commands wrong. What is the difference between sleep() & time.sleep()? – Tom Paquette Apr 20 '19 at 22:36
  • I've made an edit to explain the difference, Tom. – Mark Smith Apr 25 '19 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.