random模块、time模块、sys模块、os模块
一、random模块
1.随机取小数 (数学计算)
print(random.random()) #取0-1之间的小数
print(random.uniform(3,6)) #uniform(n,m)取一个范围之间的小数
2.取随机整数 (抽奖)
print(random.randint(1,2)) #顾头顾尾 或 [1,2]
print(random.randrange(1,2)) #顾头不顾尾 [1,2)
print(random.randrange(1,100,2)) #取1-99之间的奇数
3.从一个列表中随机抽取值 (抽奖)
lis = ['a','b',(1,2,3),123,456]
print(random.choice(lis)) #从列表中随机抽取一项元素
lis = ['a','b',(1,2,3),123,456]
print(random.sample(lis,2)) #从列表中随机抽取两项元素
choice直接用两次与sample一次取两个元素的区别:
lis = ['a','b',(1,2,3),123,456]
print(random.choice(lis))
print(random.choice(lis))
print(random.sample(lis,2))
choice两次取到的值可能是相同的,而sample一次取到的两个元素是不相同的,也就是说:sample取到的值是不重复的
4.打乱一个列表的顺序,在原来列表的基础上进行修改,节省空间 (洗牌)
random.shuffle(lis)
print(lis)
二、time模块
1.时间戳时间
print(time.time())
2.结构化时间
print(time.strftime("%Y-%m-%d"))
print(time.strftime("%y-%m-%d"))
print(time.strftime('%c'))
3.时间戳时间转换成字符串时间(格式化时间)
cishi = time.time() #此刻的时间戳
struct_time = time.localtime(cishi) #将时间戳时间转成结构化时间
timestamp = time.strftime("%Y-%m-%d %H:%M:%S",struct_time) #将结构化时间转成格式化时间
print(timestamp)
4.将字符串时间转换成时间戳时间
struct_time = time.strptime("2018-8-20","%Y-%m-%d") #将格式化时间转换成结构化时间
print(time.mktime(struct_time)) #再转换成时间戳时间
5.练习题
(1) 查看一下2000000000时间戳时间表示的年月日
struct_time = time.localtime(2000000000)
result = time.strftime("%Y-%m-%d",struct_time)
print(result)
(2) 将2018-8-20换成时间戳时间
struct_time = time.strptime("2018-8-20","%Y-%m-%d")
print(time.mktime(struct_time))
(3) 请将当前时间的当前月1号的时间戳时间取出来 - 函数
def get_time():
struct_time = time.localtime()
ret = time.strptime("%s-%s-1"%(struct_time.tm_year,struct_time.tm_mon),"%Y-%m-%d")
return time.mktime(ret)
print(get_time())
(4) 计算时间差 - 函数 2018-8-19 22:10:8 2018-8-20 11:07:3 经过了多少时分秒
t1 = '2018-8-19 22:10:8'
t2 = '2018-8-20 11:07:3'
struct_time1 = time.strptime(t1,'%Y-%m-%d %H:%M:%S')
struct_time2 = time.strptime(t2,'%Y-%m-%d %H:%M:%S')
timestamp1 = time.mktime(struct_time1)
timestamp2 = time.mktime(struct_time2)
result = timestamp2-timestamp1
gm_time = time.gmtime(result) #伦敦时间
print('过去了%d年%d月%d天%d小时%d分钟%d秒'%(gm_time.tm_year-1970,gm_time.tm_mon-1, gm_time.tm_mday-1,gm_time.tm_hour,gm_time.tm_min,gm_time.tm_sec))
三、sys模块 (是和python解释器打交道的)
1.sys.argv
print(sys.argv) #argv是python这个命令后面的值
usr = sys.argv[1]
pwd = sys.argv[2]
if usr == 'alex' and pwd=='alex3741':
print("登陆成功")
else:
exit()
2.sys.path
3.sys.modules
print(sys.modules)
print(sys.modules['re'].findall("\d+","abc123")) #我们导入内存中所有模块的名字,这个模块的内存地址
四、os模块 (是和操作系统交互的模块)
1.
os.makedirs('dir1/dir2')
os.mkdir('dir3')
os.mkdir('dir3/dir4')
2.只能删空文件夹
os.rmdir('dir3/dir4')
os.removedirs('dir3/dir4')
os.removedirs('dir1/dir2')
3.程序要处理这些路径
ret = os.popen('dir) # 是和做查看类的操作
s =ret.read()
print(s)
print(s.split('\n'))
os.listdir / os.path.join
file_lst = os.listdir('D:\sylar\s15')
for path in file_lst:
print(os.path.join('D:\sylar\s15',path))
print('-->',os.getcwd()) # current work dir 当前工作目录
并不是指当前文件所在的目录
当前文件是在哪个目录下执行的
ret = os.popen('dir') # 是和做查看类的操作
s =ret.read()
print(s)
os.chdir('D:\sylar\s15\day18') # 切换当前的工作目录
ret = os.popen('dir') # 是和做查看类的操作
s =ret.read()
print(s)
random模块、time模块、sys模块、os模块的更多相关文章
- python note 17 random、time、sys、os模块
1.random模块(取随机数模块) # 取随机小数 : 数学计算 import random print(random.random())# 取0-1之间的小数 print(random.unifo ...
- python中sys和os模块的使用
在python中,sys,os模块是非常强大的,提供了许多对文件夹.文件和路径的操作方法 sys模块 sys.argv #命令行执行脚本,其实它就是一个列表 ,sys.argv[0] 是程序自身路 ...
- Python学习日记(八)—— 模块一(sys、os、hashlib、random、time、RE)
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
- 【Python】 sys和os模块
sys sys模块能使程序访问于python解释器联系紧密的变量和函数 ● sys中的一些函数和变量 argv 命令行参数构成的列表 path 查找所有可用模块所在的目录名的列表 platform 查 ...
- sys、os 模块
sys 模块常见函数 sys.argv #命令行参数List,第一个元素是程序本身路径 sys.exit(n) #退出程序,正常退出时exit(0) sys.vers ...
- python的sys和os模块
一.sys sys.argv:实现从程序外部向程序传递参数. 其中sys.argv[0]为脚本的名称,所以要判断是否有参数传入可以:if len(sys.argv) > 1. sys.exi ...
- python笔记6 模块与包 程序开发规范 包 re sys time os模块
模块与包 python 模块首引用加载到内存,如果再次引用此模块,直接从内存中读取. python文件分为:执行文件(解释器运行的文件),被引用文件(import) 模块引用一共发生了3件事: 1.他 ...
- python学习笔记:sys、os模块
os模块:负责程序与操作系统的交互,提供了访问操作系统底层的接口; sys模块:负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境. --os 常用方法-- ...
- Python中sys和os模块的区别
sys: This module provides access to some variables used or maintained by the interpreter and to func ...
- 【Python】【有趣的模块】【sys&time&os】
[模块] sys.path.append('C:/Users/wangxue1/PycharmProjects/selenium2TestOne') 然后就可以直接import 这个路径下的模块了 [ ...
随机推荐
- “扩展域”与"边带权"的并查集
https://www.luogu.org/problemnew/show/P1196 银河英雄传说 #include<bits/stdc++.h> using namespace std ...
- jenkins-参数化构建(三)插件:Git Parameter
一.下载插件Git Parameter (更加省事) 在配置中branch和tag用的比较多 注意:Credential 可以添加密码,jenkins如果在root用户下载的请改 /etc/sys ...
- 如何安装Magento 2.0
//来源:http://www.360magento.com/blog/install-magento2 如何安装Magento 2.0 2015/8/11 下午4:23 发布者: shi yong ...
- js图片预加载与延迟加载
图片预加载的机制原理:就是提前加载出图片来,给前端的服务器有一定的压力. 图片延迟加载的原理:为了缓解前端服务器的压力,延缓加载图片,符合条件的时候再加载图片,当然不符合的条件就不加载图片. 预加载 ...
- Wpf DataGrid 自动滚动到最后一行
if (mainDataGrid.Items.Count > 0) { var border = VisualTreeHelper.GetChild(mainDataGrid, 0) as De ...
- 1. Scala概述
1.1 概述 联邦理工学院洛桑(EPFL)的Martin Odersky于2001年开始设计Scala Scala是Scalable Language的简写,是一门多范式的编程语言 1.2 Scala ...
- 关于ico图标
ico图标可以作为网页标签上显示的小logo,比如: 要获取一个网站的ico图标,只需要在url后输入/favicon.ico即可,比如 https://www.baidu.com/favicon ...
- linux软件安装方式
先插句题外话,快捷键 Ctrl+s 的功能是停止输入,Ctrl+q 恢复输入; 正题,在linux的应用软件安装有三种: 1,tar包 2,rpm包 3,dpkg包 以下介绍三种包的安装和卸载方式 1 ...
- js动态改变setInterval的时间
<!DOCTYPE html> <html> <head> <!--meta name="viewport" content=" ...
- spring-session 2.0 实现细节
一. 前置知识 1. redis 在键实际过期之后不一定会被删除,可能会继续存留 2. 具有过期时间的 key 有两种方式来保证过期 一是这个键在过期的时候被访问了 二是后台运行一个定时任务自己删除过 ...