让Python脚本暂停执行的几种方法(小结)

yipeiwu_com5年前Python基础

1.time.sleep(secs)

参考文档原文:

Suspend execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal's catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

大意:

让程序执行暂停指定的秒数,参数可以是浮点型以指定精确的时间,但是程序真正暂停的时间可能长于请求的时间也可能短于暂停的时间。

2. raw_input( )

通过等待输入来让程序暂停

3. os.system("pause")

通过执行操作系统的命令来让程序暂停,该函数是通过实现标准C函数system( )来实现的。

Python2.4新加入了subprocess模块,而且官方建议使用改模块替换os.system所以,也可以这样写:

subprocess.call("pause",shell=True)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中的测试模块unittest和doctest的使用教程

我要坦白一点。尽管我是一个应用相当广泛的公共域 Python 库的创造者,但在我的模块中引入的单元测试是非常不系统的。实际上,那些测试大部分 是包括在 gnosis.xml.pickle...

python+selenium实现163邮箱自动登陆的方法

python+selenium实现163邮箱自动登陆的方法

本文介绍了 让我们先来预览一下代码运行效果吧: 首先分析163邮箱登陆页面的网页结构(按F12或单击鼠标右键选择审查元素) 1、定位到登陆框(注意登录框是一个iframe,如果不定位...

Python+matplotlib+numpy绘制精美的条形统计图

Python+matplotlib+numpy绘制精美的条形统计图

本文实例主要向大家分享了一个Python+matplotlib+numpy绘制精美的条形统计图的代码,效果展示如下: 完整代码如下: import matplotlib.pyplo...

python频繁写入文件时提速的方法

问题背景:有一批需要处理的文件,对于每一个文件,都需要调用同一个函数进行处理,相当耗时。 有没有加速的办法呢?当然有啦,比如说你将这些文件分成若干批,每一个批次都调用自己写的python...

Python写的贪吃蛇游戏例子

第一次用Python写这种比较实用且好玩的东西,权当练手吧 游戏说明: * P键控制“暂停/开始”* 方向键控制贪吃蛇的方向 源代码如下:复制代码 代码如下:from Tkinter i...