4

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
TylerTotally
  • 113
  • 1
  • 2
  • 9

2 Answers2

5

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
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
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

Community
  • 1
  • 1
lochsh
  • 366
  • 1
  • 12
  • 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: https://www.python.org/dev/peps/pep-0008/ – lochsh Mar 14 '16 at 21:49
  • I was going to use this for two programs so also thank you for responding because you also fixed both of my problems – TylerTotally Mar 14 '16 at 21:52
  • Thanks TylerTotally :) Maybe you can give me an upvote if you liked my answer ;o – lochsh Mar 14 '16 at 21:53
  • 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 – TylerTotally Mar 14 '16 at 21:56
  • Good luck on your Python journey! It's so much easier to read than JS in my opinion! Just give yourself a little time :) – lochsh Mar 14 '16 at 22:22