Running a .py file in a loop
Solution 1
There are a few reasons why your code isn't working:
- Incorrect indentation (this may just be how you copied it on to StackOverflow though).
- Using
os
without importing it. - Not using quotes for a string.
- Mis-using the
open
function;open
opens a file for reading and/or writing. To execute a file you probably want to use theos.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
TylerTotally
Updated on June 04, 2022Comments
-
TylerTotally almost 2 years
I am currently trying to run a
.py
file but in a loop. Just for a test I am usingI = 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 about 8 yearsWhat error are you getting?
-
TylerTotally about 8 yearsI would have to go back and look
-
DanielGibbs about 8 yearsWhen 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 about 8 yearsthank you for responding so fast by the way.
-
TylerTotally about 8 yearsill run back and look
-
-
lochsh about 8 yearsI 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 about 8 yearsI was going to use this for two programs so also thank you for responding because you also fixed both of my problems
-
lochsh about 8 yearsThanks TylerTotally :) Maybe you can give me an upvote if you liked my answer ;o
-
TylerTotally about 8 yearsNo 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 about 8 yearsGood luck on your Python journey! It's so much easier to read than JS in my opinion! Just give yourself a little time :)