当前位置:网站首页>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)
边栏推荐
- spark调优(三):持久化减少二次查询
- Inner monologue of accidental promotion
- Personal notes of graphics (2)
- Vs tool word highlight with margin
- Have fun | latest progress of "spacecraft program" activities
- Opportunity interview experience summary
- laravel构造函数和中间件执行顺序问题
- 47_Opencv中的轮廓查找 cv::findContours()
- Xcode Revoke certificate
- Tidb cannot start after modifying the configuration file
猜你喜欢
随机推荐
【PHP】PHP接口继承及接口多继承原理与实现方法
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
AutoLISP series (3): function function 3
three. JS create cool snow effect
华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
[Android -- data storage] use SQLite to store data
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
three.js打造酷炫下雪效果
HAVE FUN | “飞船计划”活动最新进展
PHP has its own filtering and escape functions
目标跟踪常见训练数据集格式
php 自带过滤和转义函数
Set the route and optimize the URL in thinkphp3.2.3
PHP realizes wechat applet face recognition and face brushing login function
打造All-in-One应用开发平台,轻流树立无代码行业标杆
Unity3d click events added to 3D objects in the scene
Imitate the choice of enterprise wechat conference room
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
二叉搜索树(基操篇)
Laravel post shows an exception when submitting data