PDA

View Full Version : Get a loop in python



PaulNL
March 7th, 2013, 02:47 PM
Hello All i want to loop this script til i stop it. how can i do that ??

i have made



import os
import time

os.system("clear")
os.system("sudo virsh -c qemu:///system list")



Paul

r-senior
March 7th, 2013, 03:02 PM
Something like this?



import time

while True:
print 'Hello'
time.sleep(1)

PaulNL
March 7th, 2013, 03:08 PM
when i use that i get an error IndentationError: expected an idented block

i have made a second script to work with the first script



while true;
do python status.py ;
sleep 5s ;
done


i cant get it all in the same file

r-senior
March 7th, 2013, 03:31 PM
Just do it in Python, there's no need for a calling script.

Python needs the content of the loop indented consistently, i.e. the same number of spaces for each line within the indent.


#!/usr/bin/env python

import os
import time

while True:
os.system("clear")
os.system("sudo virsh -c qemu:///system list")
time.sleep(5)


I've added the shebang line at the top so it runs as a script. Don't forget to 'chmod +x'.

PaulNL
March 7th, 2013, 03:46 PM
when i use your script i get an error se thread above

schragge
March 7th, 2013, 04:04 PM
The TAB/space characters could get messed up when pasting from the browser window. Delete the spaces, and re-insert them manually.

PaulNL
March 7th, 2013, 04:08 PM
Works :) Thanks