循环运行 .py 文件 [英] Running a .py file in a loop
本文介绍了循环运行 .py 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试运行 .py
文件,但在循环中.仅用于我正在使用的测试
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.
推荐答案
您的代码无法正常工作的几个原因:
There are a few reasons why your code isn't working:
- 不正确的缩进(尽管这可能只是您将其复制到 StackOverflow 的方式).
- 使用
os
而不导入它. - 不对字符串使用引号.
- 误用
open
函数;open
打开一个文件进行读取和/或写入.要执行一个文件,您可能想要使用os.system
.
- 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
这篇关于循环运行 .py 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文