Python库的使用,怎么调用库呀

头两个用random库,后四个用OS 和datetime库,能不能帮助一下,谢谢了

img

img

img

img

img

1

import random
persons=[(111,'张三',False),
         (222,'李四',True),
         (333,'王五',False),
         (444,'钱六',False),
         (555,'赵大',False)]
l=len(persons)
lst=[]
max=3
kv={}
while max>0:
    index=random.randint(0,l-1)
    if not persons[index][2] and index not in kv:
        lst.append(persons[index])
        kv[index]=True
        max-=1
print(lst)

2

def getPassword(n=10,numProb=.2):
    chrs=list("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    weights=[]
    chrProb=1-numProb
    for c in chrs:
        if c.isnumeric():
            weights.append(numProb)
        else:
            weights.append(chrProb)
    return ''.join(random.choices(chrs,weights=weights,k=n))

print(getPassword())

3

import os
import datetime
d=os.getenv('SystemDrive')
user=os.getlogin()
path=r'{}\users\{}\Desktop'.format(d,user)
path=os.path.join(path,datetime.datetime.now().strftime('%Y%m%d-%H%M%S.%f'))
os.mkdir(path)

from datetime import date

def lefttDays(dateFrom,dateTo):
    return (dateTo-dateFrom).days

newYearDay=date(date.today().year+1,1,1)
daysLeft=lefttDays(date.today(),newYearDay)

print('距离元旦还有{}天'.format(daysLeft))

4

import os
def removeFilesByExt(dir,exts,recusion=True):

    for root,dirs,files in os.walk(dir):
        for file in files:
            ext="."+file.lower().split('.')[-1]
            if ext in exts:
                os.remove(os.path.join(root,file))
        if not recusion:
            break

5

import os
import shutil
def archivve(dir):
    for root,dirs,files in os.walk(dir):
        for file in files:
            d=os.path.join(dir,file.split('_')[0])
            if not os.path.exists(d):
                os.mkdir(d)
            shutil.move(os.path.join(root,file),os.path.join(d,file))
        break

帮忙看看咋做呗

  • 请看👉 :Python常用模块的常用方法介绍 os、math、random、time、datetime、国内常见镜像
  • 除此之外, 这篇博客: python的学习记录中的 七、random模块和time模块 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    1. random模块
    • random() 生成一个0到1的随机数 [0,1)
    • random(a,b) 生成一个整数,范围:[a,b]
    • uniform(a,b) 生成一个浮点数范围:[a,b]
    • shuffle() 用于列表中的元素打乱
    • sample() 指定序列中随机获取指定长度的片段,不会修改原有序列
      -
    • choice() 从序列中获取一个随机元素
    • randrange(a,b,c) 在[a,b]上以c递增的集合,随机获取一个数
      在这里插入图片描述
      c可以在[1,3,5,7,9]中随机选一个数
    1. time模块 帖子1
    函数说明
    time()返回当前时间戳
    sleep()线程推迟指定时间运行,单位秒
    localtime()有一个时间戳转换成但钱地区的struct_time,参数未提供时,以当前时间为准
    gmtime()和localtime方法类似,gmtime()将一个时间戳转换为UTC时区的struct_time
    mktime()将一个struct_time转化为时间戳,从(1970.1.1的0点)起点 eg:time.mktime(time.localtime())
    asctime([t])把表示时间的元组或struct_time表示为这种形式“Sun Jun 20 23:01:05 2016” 如果没有参数,将time.localtime()作为参数传入
    strftime(format[ , t] )将一个表示时间的元组和或struct_time(由gmtime()和localtime()函数返回)转化为时间字符串,如果 t 未指定,将localtime()传入
    ctime([secs])把一个时间戳(按秒计算浮点数)转化为time。asctime()的形式,如果参数未给,将time.time()传入
    strptime(string[ , format])把一个格式化时间字符串转化成为struct_time。实际上它是strftime逆过程
    perf_counter()返回性能计数器的值,以秒为单位
    process_time()返回当前进程使用CPU的时间,以秒为单位
    timezone()返回本地时区的时间偏移,以秒为单位
    tzname()返回本地时区的名字

    时间格式等函数应用实例,西瓜很甜作者
    在这里插入图片描述
    time和datetime的探索;

import 库名
库名.方法
具体方法的调用,要根据该方法的接口来传参。如产生0-1之间的随机数:

import random
number = random.uniform(0, 1)
print("产生的随机数是:", number)