当前位置:网站首页>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)边栏推荐
- Reptile combat (VII): pictures of the king of reptiles' heroes
- How does win11 unblock the keyboard? Method of unlocking keyboard in win11
- Usage of MySQL subquery keywords (exists)
- JNI primary contact
- Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
- POJ 3140 contents division "suggestions collection"
- Le capital - investissement est - il légal en Chine? C'est sûr?
- Which financial products will yield high returns in 2022?
- [open source] Net ORM accessing Firebird database
- The little money made by the program ape is a P!
猜你喜欢

How to turn on win11 game mode? How to turn on game mode in win11

双塔模型的最强出装,谷歌又开始玩起“老古董”了?

Open source OA development platform: contract management user manual

强化学习-学习笔记9 | Multi-Step-TD-Target

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?
SQL injection error report injection function graphic explanation

648. Word replacement

ISO 26262 - considerations other than requirements based testing

Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve

Build your own website (18)
随机推荐
The cyberspace office announced the measures for data exit security assessment, which will come into force on September 1
Problems encountered in installing mysql8 for Ubuntu and the detailed installation process
Build your own website (18)
cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
How to make agile digital transformation strategy for manufacturing enterprises
Virtual machine network configuration in VMWare
EasyUI date control emptying value
Jenkins user rights management
gridView自己定义做时间排版「建议收藏」
Which financial products will yield high returns in 2022?
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
Win11时间怎么显示星期几?Win11怎么显示今天周几?
Win11游戏模式怎么开启?Win11开启游戏模式的方法
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
Goal: do not exclude yaml syntax. Try to get started quickly
NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
JNI primary contact
Jerry's configuration of TWS cross pairing [article]