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

yipeiwu_com6年前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解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2(char 1)

前言 在Python中提供了json包来方便快捷的解析json字串的转换过程,但是碰到了一个比较奇怪的问题,就是不太正确的json串如何来解析? 1. 问题的提出 今天在处理一个http...

Python进程间通信之共享内存详解

前一篇博客说了怎样通过命名管道实现进程间通信,但是要在windows是使用命名管道,需要使用python调研windows api,太麻烦,于是想到是不是可以通过共享内存的方式来实现。查...

Python程序设计入门(4)模块和包

Python语言功能非常强大,除了类之外,还有模块和包的概念,这有点像perl,此处简单说说包和模块。 一、Python中的模块 模块——其实就是我们说的库(lib)的概念,不过它不仅只...

Python基础教程之内置函数locals()和globals()用法分析

本文实例讲述了Python基础教程之内置函数locals()和globals()用法。分享给大家供大家参考,具体如下: 1. 这两个函数主要提供,基于字典的访问局部变量和全局变量的方式。...

pytorch中的上采样以及各种反操作,求逆操作详解

pytorch中的上采样以及各种反操作,求逆操作详解

import torch.nn.functional as F import torch.nn as nn F.upsample(input, size=None, scale_fact...