day22作业

# 1、检索文件夹大小的程序,要求执行方式如下
# python3.8 run.py 文件夹
import os,sys
src_file=sys.argv[1]
def file(src_file):
    count = 0
    for line in os.listdir(src_file):
        if os.path.isdir(line):
            file(line)
        count += os.path.getsize(line)
    print(count)
file(src_file)

# 2、随机验证码、模拟下载以及打印进度条、文件copy脚本
# 随机验证码
import random
def stochastic(n):
    res = ''
    for i in range(n):
        s1 = chr(random.randint(97,122))
        s2 = str(random.randint(0,9))
        res += random.choice([s1,s2])
    print(res)
stochastic(8)

# 模拟下载以及打印进度条
import time
def progress(percent):
    if percent > 1:
        percent = 1

    res = int(50 * percent) * '#'
    print('
[%-50s] %d%%' % (res,int(100*percent)), end='')

down_size = 0
data_size = 333333
while down_size < data_size:
    down_size += 1024
    time.sleep(0.05)
    percent = down_size / data_size
    progress(percent)
# 文件copy脚本 python3.8 run.py src_file dst_file
import sys
src_file=sys.argv[1]
dst_file=sys.argv[2]

with open(r'%s' %src_file,mode='rb') as read_f,
    open(r'%s' %dst_file,mode='wb') as write_f:
    for line in read_f:
        write_f.write(line)
原文地址:https://www.cnblogs.com/yding/p/12601332.html