当前位置:网站首页>os、sys、random标准库主要功能
os、sys、random标准库主要功能
2022-07-07 14:24:00 【努力卷】
os标准库
import os
- 系统相关变量和操作
- 文件和目录相关操作
- 执行命令和管理进程
1.系统相关变量和操作
os.environ 输出环境变量
其它的上节已有
2.文件和目录相关操作
os.rename 重命名
os.path.isabs(path) 判断是否是绝对路径
os.path.isdir(path) 判断是否是目录
os.path.isfile(path) 判断是否是文件
os.path.getatime(file) 文件最后访问时间
os.path.getsize(file) 文件大小
3.执行命令和管理进程
os.system("helloWorld.py") 执行当下目录下的文件
sys标准库
import sys
针对Python解释器相关的变量和方法
sys.version 当前python版本号
sys.maxsize 当前python能够表示的最大int
sys.path python的相关路径
sys.platform 运行的平台
sys.copyright 版权信息
sys.argv 参数 输出格式为列表
sys.exit(num) 退出 状态码
sys.getdefaultencoding() 输出默认编码方式
random标准库
import random
主要用于生成伪随机数
生成随机整数
random.randint(1,100) 随机生成一个1到100之间的数
random.randrange(1,101,2) 随机生成一个1到100之间的数 步长为2
生成随机浮点数
random.random() 随机生成一个0.0到1.0之间的浮点数
random.uniform(11.1, 13.1) 随机生成一个11.1到11.3之间的浮点数 精度高
非数字类型的随机抽样
targetList = ['a','b','c']
random.choice(targetList) 从中随机抽取一个
乱序
random.shuffle(targetList) 将列表里的数据打乱 不能用到元组
样本
random.sample(targetList,2) 在targetList样本中随机抽取2两个 顺序也会打乱但是原列表顺序不变
生成类似随机密码的字符串
字符串只包括字母和数字 可以指定生成的位数
import random,string
def gen_random_string(length):
# 随机生成字母和数字的位数
numcount = random.randint(1, length-1)
lettercount = length-numcount
# string.digits 为0123456789
# 循环抽取0-9之间的数字numcount次
numlist = [random.choice(string.digits) for _ in range(numcount)]
# string.ascii_latters 为 所有大小写字母
# 循环抽取大小写字母lettercount次
letterlist = [random.choice(string.ascii_latters) for _ in range(lettercount)]
alllist = numlist + letterlist
# 乱序
random.shuffle(alllist)
# 生成目标结构字符串
result = "".join([i for i in alllist])
return result
randString = gen_random_string(10)
print(randString)边栏推荐
- 数据中台落地实施之法
- You Yuxi, coming!
- The difference and working principle between compiler and interpreter
- Tragedy caused by deleting the console statement
- 分类模型评价标准(performance measure)
- 01tire+ chain forward star +dfs+ greedy exercise one
- IP地址和物理地址有什么区别
- 47_Opencv中的轮廓查找 cv::findContours()
- 爬虫(17) - 面试(2) | 爬虫面试题库
- PHP realizes wechat applet face recognition and face brushing login function
猜你喜欢
随机推荐
记一次项目的迁移过程
php 自带过滤和转义函数
Logback日志框架第三方jar包 免费获取
Tragedy caused by deleting the console statement
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
Asyncio concept and usage
Personal notes of graphics (3)
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
Record the migration process of a project
作为Android开发程序员,android高级面试
记录Servlet学习时的一次乱码
The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
【PHP】PHP接口继承及接口多继承原理与实现方法
水平垂直居中 方法 和兼容
模拟Servlet的本质
Three. JS series (3): porting shaders in shadertoy
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
IP地址和物理地址有什么区别
Iptables only allows the specified IP address to access the specified port
【DesignMode】模板方法模式(Template method pattern)




![[C language] question set of X](/img/17/bfa57de183c44cf0a3c6637bb65a9d.jpg)




