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.

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,

Chris

Edited 3 Years Ago by happygeek: fixed formatting

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:
   MyScript.main()
   time.sleep(30)

and for MyScript.py

def main():
   #your stuff here

if __name__ == '__main__':
   main()

hope this helps

Chris

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

do something like this

if __name__ == '__main__':
   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!"
else:
   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.

Chris

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
   myScriptsContents()
   time.sleep(30)

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

Chris

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():

	USERNAME="************"
	PASSWORD="********"

	PROTO="https://"
	SERVER="mail.google.com"
	PATH="/gmail/feed/atom"

	passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
	passman.add_password(None, SERVER, USERNAME, PASSWORD)
	authhandler = urllib2.HTTPBasicAuthHandler(passman)
	opener = urllib2.build_opener(authhandler)
	urllib2.install_opener(opener)
	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:
	time.sleep(10)
	os.system("cls")
	checkMail()
This article has been dead for over six months. Start a new discussion instead.