告诉 python 等待/暂停 for 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24409861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tell python to wait/pause a for loop
提问by user3715675
I have a python program that navigates me to a website with a function that i have defined as nav(a,b), and on this site I will be downloading some pyfits data for use on another script. This site has a different pyfits file for every set of (a,b) in a catalog I have.
我有一个 python 程序,可以将我导航到一个带有我定义为 nav(a,b) 的函数的网站,在这个网站上,我将下载一些 pyfits 数据以用于另一个脚本。对于我拥有的目录中的每组 (a,b),该站点都有一个不同的 pyfits 文件。
I was wondering if I could iterate through this catalog using a for loop, and each time the nav(a,b) function is used, tell python to pause while I download the file, then resume again when I tell it to. I've done something like this in idl before, but dont know how with python.
我想知道是否可以使用 for 循环遍历这个目录,每次使用 nav(a,b) 函数时,告诉 python 在我下载文件时暂停,然后在我告诉它时再次恢复。我以前在 idl 中做过类似的事情,但不知道如何使用 python。
Otherwise I guess im stuck runing the program 200 times, replacing the (a,b) values each time, which will take for ever.
否则我想我会坚持运行程序 200 次,每次都替换 (a,b) 值,这将永远持续下去。
采纳答案by Mattias Backman
If you want to wait for a manual signal to continue, wait for the user to press Enter:
如果要等待手动信号继续,请等待用户按 Enter:
Python 2:
蟒蛇2:
raw_input("Press Enter to continue...")
Python 3:
蟒蛇3:
input("Press Enter to continue...")
If you can download the file in the python code, do that instead of doing the manual task for each of the files.
如果您可以下载 python 代码中的文件,请执行此操作,而不是为每个文件执行手动任务。
回答by sundar nataraj
you can use time.sleep()to pause the execution for t seconds
您可以使用time.sleep()暂停执行 t 秒
import time
time.sleep(1.3) # seconds
demo:
演示:
import time
print "Start Time : %s" % time.ctime()
time.sleep( 5 )
print "End Time: %s" % time.ctime()
output
输出
Start Time: Tue Feb 17 10:19:18 2009
End Time: Tue Feb 17 10:19:23 2009
回答by James Hunt
Why not use the input function? It will pause the program until the prompt has been closed/filled in.
为什么不使用输入功能?它将暂停程序,直到提示已关闭/填写。
回答by jrjc
Why don't you try a while loop waiting for your download to finish ?
为什么不尝试等待下载完成的 while 循环?
for ... :
nav(a,b)
while downloading_not_finished:
time.sleep(X)
So, every X period of time a condition is tested, and is tested again until the downloading part is finished.
因此,每 X 时间段测试一个条件,并再次测试,直到下载部分完成。
回答by OverRatedProgrammer
Okay, Here's two ways to pause in python.
好的,这是在python中暂停的两种方法。
1) You can use the input function.
1) 您可以使用输入功能。
#Python 2
raw_input("Downloading....")
#Python 3
input("Downloading....")
This will pause the program until the user presses enter etc..
这将暂停程序,直到用户按 Enter 等。
2) You can use the time.sleep() function.
2) 您可以使用 time.sleep() 函数。
import time
time.sleep(# of seconds)
This will pause the python script for however many seconds you want.
这将暂停 python 脚本你想要的几秒钟。
回答by OverRatedProgrammer
For Python ShellIn Design:
对于设计中的Python Shell:
import sys
from time import sleep
try:
shell = sys.stdout.shell
except:
print('Run It In Shell')
dots = '........';
shell.write('Downloading')
sleep(0.5)
for dot in dots:
shell.write(dot)
sleep(0.1)
shell.write('\n')
sleep(0.4)
shell.write('Saving Files')
sleep(0.5)
for doot in dots:
shell.write(dot)
sleep(0.1)
shell.write('\n')
sleep(0.4)
For Python Console:
对于Python 控制台:
from time import sleep
print('Downloading')
sleep(1)
print('Saving Files')
sleep(1)