Results 1 to 7 of 7

Thread: Get a loop in python

  1. #1
    Join Date
    Jan 2010
    Beans
    45
    Distro
    Ubuntu 13.04 Raring Ringtail

    Get a loop in python

    Hello All i want to loop this script til i stop it. how can i do that ??

    i have made

    Code:
    import os
    import time
    
    os.system("clear")
    os.system("sudo virsh -c qemu:///system list")
    Paul
    Last edited by PaulNL; March 7th, 2013 at 04:09 PM. Reason: add code

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Get a loop in python

    Something like this?

    Code:
    import time
    
    while True:
        print 'Hello'
        time.sleep(1)
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    Jan 2010
    Beans
    45
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: Get a loop in python

    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

    Code:
    while true;
    do python status.py ;
    sleep 5s ;
    done
    i cant get it all in the same file
    Last edited by PaulNL; March 7th, 2013 at 03:22 PM.

  4. #4
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Get a loop in python

    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.

    Code:
    #!/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'.
    Last edited by r-senior; March 7th, 2013 at 03:33 PM.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  5. #5
    Join Date
    Jan 2010
    Beans
    45
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: Get a loop in python

    when i use your script i get an error se thread above

  6. #6
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Get a loop in python

    The TAB/space characters could get messed up when pasting from the browser window. Delete the spaces, and re-insert them manually.

  7. #7
    Join Date
    Jan 2010
    Beans
    45
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: Get a loop in python

    Works Thanks
    Last edited by PaulNL; March 7th, 2013 at 04:08 PM. Reason: Case Closed

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •