Running a .py file in a loop

15,076

Solution 1

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

Solution 2

  • Python is indentation-sensitive, and your code is missing indentation after the while statement!

  • Running the open command will not run the Python script. You can read what it does here in the docs: https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

  • This stack overflow question talks about how to run Python that's stored in another file How can I make one python file run another?

    I recommend wrapping the code you want to run in a function, e.g.

     def foo():
         print 'hello'
    

    and then saving this in foo.py. From your main script, you can then do:

    import foo
    
    i = 0
    while i < 10:
        foo.foo()
        i += 1
    
  • If you want to run something in an infinite loop, you need the condition for the while loop to always be true:

    while True:
        # do thing forever
    
  • A note on importing: The example I have given will work if the foo.py file is in the same directory as the main Python file. If it is not, then you should have a read here about how to create Python modules http://www.tutorialspoint.com/python/python_modules.htm

Share:
15,076
TylerTotally
Author by

TylerTotally

Updated on June 04, 2022

Comments

  • TylerTotally
    TylerTotally almost 2 years

    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.

    • DanielGibbs
      DanielGibbs about 8 years
      What error are you getting?
    • TylerTotally
      TylerTotally about 8 years
      I would have to go back and look
    • DanielGibbs
      DanielGibbs about 8 years
      When you post questions on StackOverflow it's usually helpful to include any errors you have to make it easier for people to help you.
    • TylerTotally
      TylerTotally about 8 years
      thank you for responding so fast by the way.
    • TylerTotally
      TylerTotally about 8 years
      ill run back and look
  • lochsh
    lochsh about 8 years
    I changed your variable I to i, as it is generally considered bad to have variables with capital letters in python, see the style guide: python.org/dev/peps/pep-0008
  • TylerTotally
    TylerTotally about 8 years
    I was going to use this for two programs so also thank you for responding because you also fixed both of my problems
  • lochsh
    lochsh about 8 years
    Thanks TylerTotally :) Maybe you can give me an upvote if you liked my answer ;o
  • TylerTotally
    TylerTotally about 8 years
    No problem! obviously I am very new to this judged by my repetition and my python programing ability. My "mother tongue" is JavaScript so this is all new waters
  • lochsh
    lochsh about 8 years
    Good luck on your Python journey! It's so much easier to read than JS in my opinion! Just give yourself a little time :)