python note 17 random、time、sys、os模块
1、random模块(取随机数模块)
# 取随机小数 : 数学计算
import random
print(random.random())# 取0-1之间的小数
print(random.uniform(1,2))# 取1-2之间的小数
# 取随机整数 : 彩票 抽奖
import random
print(random.randint(1,2)) #头和尾都取得到
print(random.randrange(1,2)) # 取不到尾部
print(random.randrange(1,200,2)) # 按步数进行随机取数
# 从一个列表中随机抽取值 : 抽奖
l = ['a','b',(1,2),123]
print(random.choice(l))# 选择列表中一个进行随机取值,如果进行多次会取到重复的值
print(random.sample(l,2))# 选取两个值,两个值不会重复
# 打乱一个列表的顺序,在原列表的基础上直接进行修改,节省空间
l = ['a','b',(1,2),123]
random.shuffle(l)
print(l)
#验证码练习
# 4位数字验证码
import random s = ''
for i in range(4):
num = random.randint(0,9)
s += str(num)
print(s)
# 6位数字验证码
import random
def code(n = 6):
s = ''
for i in range(n):
num = random.randint(0,9)
s += str(num)
return s
print(code(6))
# 6位数字+字母验证码
import random
s = ''
for i in range(6):
num = str(random.randint(0,9))
alpha_upper = chr(random.randint(65,90))
alpha_lower = chr(random.randint(97, 122))
ret = random.choice([num,alpha_upper,alpha_lower])
s += ret
print(s)
# 函数版本
import random
def code(n = 6):
s = ''
for i in range(n):
# 生成随机的大写字母,小写字母,数字各一个
num = str(random.randint(0,9))
alpha_upper = chr(random.randint(65,90))
alpha_lower = chr(random.randint(97,122))
res = random.choice([num,alpha_upper,alpha_lower])
s += res
return s
print(code(4))
# 进阶
import random
def code(n = 6,alpha = True):
s = ''
for i in range(n):
num = str(random.randint(0,9))
if alpha:
alpha_upper = chr(random.randint(65,90))
alpha_lower = chr(random.randint(97,122))
num = random.choice([num,alpha_upper,alpha_lower])
s += num
return s
print(code(4,False))
2、time模块
# time模块主要是用来和时间打交道的
# 时间戳时间
import time
print(time.time())
# 格式化时间
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%y-%m-%d %H:%M:%S'))
print(time.strftime('%c'))
# 结构化时间
struct_time = time.localtime() # 北京时间
print(struct_time)
print(struct_time.tm_mon)
# 时间戳换成字符串时间
struct_time = time.localtime(1500000000)
ret = time.strftime('%y-%m-%d %H:%M:%S',struct_time)
print(ret)
# 字符串时间 转 时间戳
struct_time = time.strptime('2018-8-8','%Y-%m-%d')
res = time.mktime(struct_time)
print(res)
1.查看一下2000000000时间戳时间表示的年月日
struct_t = time.localtime(2000000000)
print(time.strftime('%Y-%m-%d',struct_t))
2.将2008-8-8转换成时间戳时间
t = time.strptime('2008-8-8','%Y-%m-%d')
print(time.mktime(t))
3.请将当前时间的当前月1号的时间戳时间取出来 - 函数
def get_time():
st = time.localtime()
st2 = time.strptime('%s-%s-1'%(st.tm_year,st.tm_mon),'%Y-%m-%d')
return time.mktime(st2)
print(get_time())
4.计算时间差 - 函数
# 2018-8-19 22:10:8 2018-8-20 11:07:3
# 经过了多少时分秒
str_time1 = '2018-8-19 22:10:8'
str_time2 = '2018-8-20 11:07:3'
struct_t1 = time.strptime(str_time1,'%Y-%m-%d %H:%M:%S')
struct_t2 = time.strptime(str_time2,'%Y-%m-%d %H:%M:%S')
timestamp1 = time.mktime(struct_t1)
timestamp2 = time.mktime(struct_t2)
sub_time = timestamp2 - timestamp1
gm_time = time.gmtime(sub_time)
# 1970-1-1 00:00:00
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))
3、sys模块
# sys 是和Python解释器打交道的
print(sys.argv) # argv的第一个参数 是python这个命令后面的值
#两种不同的传值
import sys
usr = input('username')
pwd = input('password')
usr = sys.argv[1]
pwd = sys.argv[2]
if usr == 'alex' and pwd == 'alex3714':
print('登录成功')
else:
exit()
# 是我们导入到内存中的所有模块的名字 : 这个模块的内存地址
import re
print(sys.modules['re'].findall('\d','abc126'))
4、os模块
# os是和操作系统交互的模块
os.makedirs('dir1/dir2')
os.mkdir('dir3') # 只能创建一个
os.mkdir('dir3/dir4') #只有3存在才能创建4
# 只能删空文件夹
os.rmdir('dir3/dir4')
os.removedirs('dir3/dir4')
os.removedirs('dir1/dir2')
# exec/eval执行的是字符串数据类型的 python代码
# os.system和 os.popen是执行字符串数据类型的 命令行代码
os.'system('dir') # 执行操作系统的命令,没有返回值,实际的操作/删除一个文件 创建一个文件夹 exec
ret = os.popen('dir) # 是和做查看类的操作
s =ret.read()
print(s)
# os.listdir()列举目录下的所有文件。返回的是列表类型。
file_lst = os.listdir('D:\sylar\s15')
for path in file_lst:
print(os.path.join('D:\sylar\s15',path))
#os.path.join()将path进行组合,若其中有绝对路径,则之前的path将被删除。
path = os.path.join('D:\\pythontest\\b', 'D:\\pythontest\\a')
print(path)
#输出D:\pythontest\a
# os.getcwd() # 查看当前工作目录
os.chdir('D:\sylar\s15\day18') # 切换当前的工作目录
ret = os.popen('dir') # 是和做查看类的操作
s =ret.read()
print(s)
# os.path.abspath()
#把路径中不符合规范的/改成操作系统默认的格式
path = os.path.abspath('D:/sylar/s15/day19/4.os模块.py')
print(path)
#能够给能找到的相对路径改成绝对路径
path = os.path.abspath('4.os模块.py')
print(path)
#os.path.split() 就是把一个路径分成两段,第二段是一个文件/文件夹
path= os.path.split('D:/sylar/s15/day19/4.os模块.py')
print(path)
#输出('D:/sylar/s15/day19', '4.os模块.py')
path= os.path.split('D:/sylar/s15/day19')
print(path)
#输出('D:/sylar/s15', 'day19')
path= os.path.split('D:/sylar/s15/day19/')
print(path)
#输出('D:/sylar/s15/day19', '')
#os.path.dirname / os.path.basename
ret1 = os.path.dirname('D:/sylar/s15/day19/4.os模块.py')
ret2 = os.path.basename('D:/sylar/s15/day19/4.os模块.py')
print(ret1) #D:/sylar/s15/day19
print(ret2) #4.os模块.py
#os.path.exists() 判断文件/文件夹是否存在
res = os.path.exists(r'D:\sylar\s15\day19\4.os模块.py')
print(res)
#其他用法
os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path) 如果path是绝对路径,返回True
os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path) 返回path所指向的文件或者目录的最后访问时间
os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
os.path.getsize(path) 返回path的大小
python note 17 random、time、sys、os模块的更多相关文章
- python 常用模块(一): random , time , sys , os模块部分知识.
1.常用模块:(1)collectiaons模块 (2)与时间相关 time模块 (3)random模块 (4)os模块 (5)sys模块 (6) 序列化模块: json , pickle 2 ...
- day18 python模块 random time sys os模块
day18 python 一.random模块 取随机整数 import random print(random.randint(1,2)) #顾头顾尾 p ...
- sys,os,模块-正则表达式
# *__conding:utf-8__* """"我是注释""" sys,os模块 import sysimport os pr ...
- day2_python的数据类型,sys,os模块,编码解码,列表,字典
今天主要了解了python的数据类型,sys,os模块,编码解码,列表,字典 1.数据类型:int(python3没有长整型)文本总是Unicode,str表示二进制用byte类表示布尔型:True( ...
- python文件、文件夹操作OS模块
转自:python文件.文件夹操作OS模块 '''一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: ...
- random,time,sys,os,序列化模块
random模块(随机数模块) 取随机小数: random.random() 取0-1之间的小数 random.uniform(x, y) 取x-y之间的小数 取随机整数: random.randin ...
- time random sys os 模块
时间模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: (1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日 ...
- time | sys | os 模块,递归删除文件,项目分析
一,复习 ''' 1.跨文件夹导包 - 不用考虑包的情况下直接导入文件夹(包)下的具体模块 2.__name__: py自执行 '__main__' | py被导入执行 '模块名' 3.包:一系列模块 ...
- random,time,sys,os
import random print(random.random()) #(0,1)大于0且小于1之间的小数 print(random.randint(1,3)) #大于等于1且小于等于3之间的整数 ...
随机推荐
- html/css/js----js中遇到的一些问题
学习前端的时候有时也会遇到一些弄不明白的问题,学习js会有更多的方法不清楚它的用法,我谨以在学习中遇到的一些问题记录下来,以便日复习! 一."window.opener.location.r ...
- IDEA注册码分享
IntelliJ IDEA IDEA 2018 激活注册码分享鼠标连续 三下左键点击 选中,再Ctrl+C 即可复制. CSDN在末尾会带上博客的说明,请删除后,复制到 IDEA中激活. 注册码激活: ...
- windows驱动开发前导知识
从以下整理得到 https://blog.csdn.net/suxinpingtao51/article/details/8610528 http://www.cnblogs.com/bugcheck ...
- Error occurred during initialization of VM Could not reserve enough space for object heap
Error occurred during initialization of VM Could not reserve enough space for object heap Java虚拟机(JV ...
- Linux高级指令
一.hostname指令 作用:操作服务器的主机名(读取,设置) #hostname 作用:表示输出完整的主机名 #hostname -f 作用:表示输出当前主机名中的FQDN(权限定域名 ...
- DLC 基本定律与规则
字母数字码 :除了数字以外,数字系统还需要处理数字以外的符号,如标点符号,控制命令等 最常见的是ASCII ASCII码是7位二进制码有128种组合,表示128个符号例如 二进制表示 十六进制表示 十 ...
- Navicat操作SQL server 2008R2文件.bak文件还原
项目操作过程中,利用Navicat操作SQL Server2008R2数据备份,结果发现数据丢失了很多,不得不先对数据丢失部分进行差异对比,然后再重新输入. 1.利用Navicat导出的数据格式为sq ...
- postman引用外部文件中的变量和数据
接口参数显示: 点击collections下文件夹test0424右边的箭头,点击run按钮: DataFile导入txt文件: 预览文件数据: 运行,成功:
- gflags 学习
一.下载 https://github.com/gflags/gflags 二.可以将gflags编译成lib 三.在需要的工程的workspace下面引入编译好的gflags动态库,在库里面写好BU ...
- H5使用codovar插件实现微信支付(微信APP支付模式,前端)
H5打包的app实现微信支付及支付宝支付,本章主要详解微信支付,支付宝支付请查看另一篇“H5使用codovar插件实现支付宝支付(支付宝APP支付模式,前端)” ps:本文只试用H5开发的,微信 AP ...