当前位置:网站首页>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)边栏推荐
- Opportunity interview experience summary
- Mysql database basic operation DQL basic query
- three. JS create cool snow effect
- markdown公式编辑教程
- How to implement backspace in shell
- JS 模块化
- three.js打造酷炫下雪效果
- 23. 合并K个升序链表-c语言
- Prometheus API deletes all data of a specified job
- Set the route and optimize the URL in thinkphp3.2.3
猜你喜欢

Leetcode-231-2的幂

Record the migration process of a project

使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑

Opencv configuration 2019vs
3000 words speak through HTTP cache

Continuous creation depends on it!
![[C language] question set of X](/img/17/bfa57de183c44cf0a3c6637bb65a9d.jpg)
[C language] question set of X

Personal notes of graphics (1)

Mysql database basic operation DQL basic query

"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
随机推荐
记一次项目的迁移过程
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
Detailed explanation of several ideas for implementing timed tasks in PHP
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
修改配置文件后tidb无法启动
二叉搜索树(基操篇)
1亿单身男女“在线相亲”,撑起130亿IPO
模拟Servlet的本质
Unity3d click events added to 3D objects in the scene
Geoserver2.18 series (5): connect to SQLSERVER database
Odoo集成Plausible埋码监控平台
Laravel5.1 Routing - routing packets
What are compiled languages and interpreted languages?
【医学分割】attention-unet
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
logback. XML configure logs of different levels and set color output
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
Laravel changed the session from file saving to database saving
【PHP】PHP接口继承及接口多继承原理与实现方法
C语言进阶——函数指针