当前位置:网站首页>Main functions of OS, Sys and random Standard Libraries
Main functions of OS, Sys and random Standard Libraries
2022-07-07 21:54:00 【Effort volume】
os Standard library
import os
- System related variables and operations
- File and directory related operations
- Execute commands and manage processes
1. System related variables and operations
os.environ Output environment variables
Other previous sections have
2. File and directory related operations
os.rename rename
os.path.isabs(path) Determine if it's an absolute path
os.path.isdir(path) Determine if it's a directory
os.path.isfile(path) Determine if it's a document
os.path.getatime(file) Last file access time
os.path.getsize(file) file size
3. Execute commands and manage processes
os.system("helloWorld.py") Execute the files in the current directory
sys Standard library
import sys
in the light of Python Interpreter related variables and methods
sys.version At present python Version number
sys.maxsize At present python Can express the largest int
sys.path python The relevant path of
sys.platform Running platform
sys.copyright Copyright information
sys.argv Parameters Output format is list
sys.exit(num) sign out Status code
sys.getdefaultencoding() Output default encoding
random Standard library
import random
It is mainly used to generate pseudo-random numbers
Generate random integer
random.randint(1,100) Randomly generate one 1 To 100 Number between
random.randrange(1,101,2) Randomly generate one 1 To 100 Number between In steps of 2
Generate random floating point numbers
random.random() Randomly generate one 0.0 To 1.0 The floating point number between
random.uniform(11.1, 13.1) Randomly generate one 11.1 To 11.3 The floating point number between High precision
Random sampling of non numeric types
targetList = ['a','b','c']
random.choice(targetList) Choose one randomly
Disorder
random.shuffle(targetList) Disarrange the data in the list Tuples cannot be used
sample
random.sample(targetList,2) stay targetList Randomly selected from the sample 2 Two The order will also be disordered, but the order of the original list will not change
Generate a string similar to random password
The string contains only letters and numbers You can specify the number of bits generated
import random,string
def gen_random_string(length):
# Randomly generate the number of digits of letters and numbers
numcount = random.randint(1, length-1)
lettercount = length-numcount
# string.digits by 0123456789
# Cyclic extraction 0-9 Number between numcount Time
numlist = [random.choice(string.digits) for _ in range(numcount)]
# string.ascii_latters by All upper and lower case letters
# Circular extraction of upper and lower case letters lettercount Time
letterlist = [random.choice(string.ascii_latters) for _ in range(lettercount)]
alllist = numlist + letterlist
# Disorder
random.shuffle(alllist)
# Generate target structure string
result = "".join([i for i in alllist])
return result
randString = gen_random_string(10)
print(randString)
边栏推荐
- Jetty: configure connector [easy to understand]
- Contour layout of margin
- [colmap] sparse reconstruction is converted to mvsnet format input
- SQL injection error report injection function graphic explanation
- Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
- Feature generation
- Jerry's fast pairing does not support canceling pairing [article]
- Song list 11111
- [uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
- Meta force force meta universe system development fossage model
猜你喜欢
Automatic classification of defective photovoltaic module cells in electronic images
2022 how to evaluate and select low code development platforms?
Magic weapon - sensitive file discovery tool
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
三元表达式、各生成式、匿名函数
Talk about relational database and serverless
Goal: do not exclude yaml syntax. Try to get started quickly
Debugging and handling the problem of jamming for about 30s during SSH login
Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘
Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
随机推荐
Unity3d 4.3.4f1执行项目
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
Win11游戏模式怎么开启?Win11开启游戏模式的方法
Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
[JDBC Part 1] overview, get connection, CRUD
嵌入式开发:如何为项目选择合适的RTOS?
The function is really powerful!
Demon daddy C
EasyUI date control emptying value
Is it safe to open an account online now? I want to know where I can open an account in Nanning now?
Solve the problem of using uni app mediaerror mediaerror errorcode -5
Restapi version control strategy [eolink translation]
Matplotlib drawing interface settings
L2:ZK-Rollup的现状,前景和痛点
The maximum number of meetings you can attend [greedy + priority queue]
建立自己的网站(18)
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
An in-depth understanding of fp/fn/precision/recall
Unity3d 4.3.4f1 execution project
FatMouse' Trade (Hangdian 1009)