Search code examples
pythonpython-2.7while-loopraspberry-pi2pause

Running a .py file in a loop


I am currently trying to run a .py file but in a loop. Just for a test I am using

I = 0
while I<10:
    os.pause(10)
    open(home/Tyler/desktop/test.py)
    I = I + 1

I am sure this is a very simple question but I can't figure this one out. I would also like to add in the very end of this I have to make this run infinitely and let it run for some other things.


Solution

  • There are a few reasons why your code isn't working:

    1. Incorrect indentation (this may just be how you copied it on to StackOverflow though).
    2. Using os without importing it.
    3. Not using quotes for a string.
    4. Mis-using the open function; open opens a file for reading and/or writing. To execute a file you probably want to use the os.system.

    Here's a version that should work:

    import os
    
    i = 0
    while i < 10:
        os.pause(10)
        os.system("home/Tyler/desktop/test.py")
        i += 1