I have a script that I would like to repeatedly run every 30 seconds, does anyone know how to do this? Also, a side question. Is it possible to have the output form the previous run replaced by the new output? Instead of it all stacking up every time the script outputs in the command prompt? Thanks for reading, and helping me learn.

Recommended Answers

All 10 Replies

perhaps you could run a different script that calls the scripts you want to run every 30 seconds, also if your on windows you could call os.system("cls") from within your script assuming you have imported the os module 'import os' this will clear the console for you. Im sure there is something similar under linux.

Hope this helps,


how can you call another python script from within one? And, do you know what function could control the timing?

do something like this

import time, MyScript
while True:

and for MyScript.py

def main():
   #your stuff here

if __name__ == '__main__':

hope this helps


It's working! I don't uderstand the importance of this part, can you explain?

do something like this

if __name__ == '__main__':

This section is to do with importing, when a file is run on its own as an indervidual script the contents of this section is run. When the file is imported this section of code isn't run, here a short example to demonstrate this.

if __name__ == '__main__':
   print "I was not imported!"
   print "I was imported!"

run this code straight and run it by importing it you will see the effects.

The reason behind this is so a standalone script can have its functions imported into other scripts without the body of the code being run.


interesting, thanks so much for the help.

Thanks, I found that helpful too!

Your more than welcome


Sorry for double post.

One thing to notice is that you can probably save yourself space here. Forexampe you have the following script

import time
def myScriptsContents():
   #some code
   #some more code

while True:  #begin your infinite loop

Notice how much simple this is but should still have the same effect.


yeah, I just did that. There is no reason to have 2 files I guess. Here is the finished script if your interested:

import time, os, urllib2, re, sys

def checkMail():



	passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
	passman.add_password(None, SERVER, USERNAME, PASSWORD)
	authhandler = urllib2.HTTPBasicAuthHandler(passman)
	opener = urllib2.build_opener(authhandler)
	page = urllib2.urlopen(PROTO + SERVER + PATH)

	for line in page:
		count = line.find("fullcount")
		if count > 0: break

	newmails = int(re.search('\d+', line).group())

	if newmails > 0: 
		if newmails > 1: print "You have ", newmails, " new Gmail messages!"
		else: print "You have a new Gmail message"
	else: print "No new mail right now :("

while True:
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.