当前位置:网站首页>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)
边栏推荐
- Devil daddy A0 English zero foundation self-improvement Road
- Jenkins user rights management
- NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?
- How to make agile digital transformation strategy for manufacturing enterprises
- [uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
- POJ 3140 Contestants Division「建议收藏」
- Mahout-Pearson correlation的实现
- Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]
- JNI primary contact
- Talk about relational database and serverless
猜你喜欢
Ten thousand word summary data storage, three knowledge points
Redis - basic use (key, string, list, set, Zset, hash, geo, bitmap, hyperloglog, transaction)
Goal: do not exclude yaml syntax. Try to get started quickly
An in-depth understanding of fp/fn/precision/recall
Jerry's test box configuration channel [chapter]
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
Talk about relational database and serverless
Jerry's manual matching method [chapter]
Win11如何解禁键盘?Win11解禁键盘的方法
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
随机推荐
Jenkins user rights management
Hoj 2245 planktonic triangle cell (Mathematics)
Lex & yacc of Pisa proxy SQL parsing
强化学习-学习笔记9 | Multi-Step-TD-Target
The cyberspace office announced the measures for data exit security assessment, which will come into force on September 1
ISO 26262 - considerations other than requirements based testing
Demon daddy A1 speech listening initial challenge
POJ 3140 Contestants Division「建议收藏」
Demon daddy guide post - simple version
How can big state-owned banks break the anti fraud dilemma?
Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
I have to use my ID card to open an account. Is the bank card safe? I don't understand it
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
[开源] .Net ORM 访问 Firebird 数据库
Unity3d 4.3.4f1执行项目
Jerry's power on automatic pairing [chapter]
Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
The function is really powerful!
Demon daddy C
特征生成