当前位置:网站首页>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)
边栏推荐
- 建立自己的网站(18)
- Solve the problem of uni in uni app Request sent a post request without response.
- Contour layout of margin
- Jerry's about TWS channel configuration [chapter]
- Restapi version control strategy [eolink translation]
- Jerry's power on automatic pairing [chapter]
- [200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
- Ant destination multiple selection
- Insufficient permissions
- OpenGL super classic learning notes (1) the first triangle "suggestions collection"
猜你喜欢
ISO 26262 - considerations other than requirements based testing
Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
An overview of the latest research progress of "efficient deep segmentation of labels" at Shanghai Jiaotong University, which comprehensively expounds the deep segmentation methods of unsupervised, ro
SQL injection error report injection function graphic explanation
Preparing for the interview and sharing experience
Ten thousand word summary data storage, three knowledge points
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
Ternary expressions, generative expressions, anonymous functions
[C language] advanced pointer --- do you really understand pointer?
随机推荐
How does win11 time display the day of the week? How does win11 display the day of the week today?
Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future
The little money made by the program ape is a P!
私募基金在中國合法嗎?安全嗎?
用语雀写文章了,功能真心强大!
你可曾迷茫?曾经的测试/开发程序员,懵懂的小菜C鸟升级......
Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
[colmap] sparse reconstruction is converted to mvsnet format input
Static test tool
Jerry's key to initiate pairing [chapter]
Lex & yacc of Pisa proxy SQL parsing
Solve the problem of using uni app mediaerror mediaerror errorcode -5
Jerry's configuration of TWS cross pairing [article]
Demon daddy B2 breaks through grammar and completes orthodox oral practice
Dry goods sharing | devaxpress v22.1 original help document download collection
Jenkins user rights management
Magic weapon - sensitive file discovery tool
Codeforces Round #275 (Div. 2) C – Diverse Permutation (构造)[通俗易懂]
The difference between NPM uninstall and RM direct deletion
Automatic classification of defective photovoltaic module cells in electronic images