当前位置:网站首页>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

  1. System related variables and operations
  2. File and directory related operations
  3. 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)

原网站

版权声明
本文为[Effort volume]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071424197059.html