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模块的更多相关文章

  1. python 常用模块(一): random , time , sys , os模块部分知识.

    1.常用模块:(1)collectiaons模块 (2)与时间相关  time模块 (3)random模块 (4)os模块 (5)sys模块 (6) 序列化模块: json  ,   pickle 2 ...

  2. day18 python模块 random time sys os模块

    day18 python   一.random模块     取随机整数 import random print(random.randint(1,2))                 #顾头顾尾 p ...

  3. sys,os,模块-正则表达式

    # *__conding:utf-8__* """"我是注释""" sys,os模块 import sysimport os pr ...

  4. day2_python的数据类型,sys,os模块,编码解码,列表,字典

    今天主要了解了python的数据类型,sys,os模块,编码解码,列表,字典 1.数据类型:int(python3没有长整型)文本总是Unicode,str表示二进制用byte类表示布尔型:True( ...

  5. python文件、文件夹操作OS模块

    转自:python文件.文件夹操作OS模块   '''一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: ...

  6. random,time,sys,os,序列化模块

    random模块(随机数模块) 取随机小数: random.random() 取0-1之间的小数 random.uniform(x, y) 取x-y之间的小数 取随机整数: random.randin ...

  7. time random sys os 模块

    时间模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: (1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日 ...

  8. time | sys | os 模块,递归删除文件,项目分析

    一,复习 ''' 1.跨文件夹导包 - 不用考虑包的情况下直接导入文件夹(包)下的具体模块 2.__name__: py自执行 '__main__' | py被导入执行 '模块名' 3.包:一系列模块 ...

  9. random,time,sys,os

    import random print(random.random()) #(0,1)大于0且小于1之间的小数 print(random.randint(1,3)) #大于等于1且小于等于3之间的整数 ...

随机推荐

  1. docker-容器,仓库

    ---恢复内容开始--- 前言: 学技术不能该断时间,连续的学习才是最好的学习方式. 00x1: 创建一个容器:docker create -it xxxx 而启动容器就有两种状态了,第一:新容器启动 ...

  2. Consul集群搭建 2Server+ 3Client

    环境说明: 192.168.202.177 consul-server01 192.168.202.177 consul-server02192.168.202.174 mysql server no ...

  3. C++调用JS函数

    1 调用方法 https://blog.csdn.net/donglinshengan/article/details/29828103 https://blog.csdn.net/sunmz_wjx ...

  4. mysql ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constrain fails

    ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constrain fails. 可能是MySQL在In ...

  5. c语言中变量和函数作用域深究

    首先,函数的作用域和访问权限基本可以参考 C语言中的作用域,链接属性和存储类型 也存在例外情况,比如内联函数 static inline,使用static 修饰 inline之后外部文件也可以访问内联 ...

  6. Python中文繁简体转换工具

    Openccpy ___ _____ __ ___ ___ ___ _____ __ __ / __`\/\ '__`\ /'__`\/' _ `\ /'___\ /'___\/\ '__`\/\ \ ...

  7. Mysql 创建用户授权

    MySQL创建用户与授权 一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用 ...

  8. ichartjs用法

    代码 <script type="text/javascript" src="../js/ichart.1.2.min.js"></scrip ...

  9. Java初学者应该注意的学习问题

    作为初学者,在刚开始学习的时候,一定会走很多弯路.但其实很多弯路是不必走的,会浪费很多时间,导致学习效率大打折扣.今天小编给大家讲述一下,作为一个Java初学者,在开始学习的时候应该注意的问题,应该从 ...

  10. inpu控件接受pipe的处理结果

    input控件绑定的变量,要接受用户的输入值,一般只要使用   [(ngModel)]  就可以. 但是,pipe处理结果如何反映到变量里去呢?不知道吧?嘿嘿 这样就可以了 :  <input ...