python每日作业 day21

今日作业:
1、检索文件夹大小的程序,要求执行方式如下python3.8 run.py 文件夹

import os,sys

file_dir=sys.argv[1]
res=0
def filedir_size(file_dir):
    global res
    for file in os.listdir(file_dir):
        filepath = os.path.join(file_dir,file)
        if os.path.isdir(filepath):
            filedir_size(filepath)
        else:
            res+=os.path.getsize(filepath)
    return res

res=filedir_size(file_dir)
print("文件大小".format(res))

2、明天上午日考:随机验证码、模拟下载以及打印进度条、文件copy脚本

import random

def make_str(size=6):
    res=""
    for i in range(size):
        s1=chr(random.randint(65,90))
        s2=str(random.randint(0,9))
        res+=random.choice([s1,s2])
    return res
res=make_str()
print(res)
随机验证码
import time


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

recv_size=0
total_size=1025011

while recv_size < total_size:
    time.sleep(0.01) # 下载了1024个字节的数据
    all_size=total_size-recv_size
    if all_size > 1024:
        recv_size+=1024 # recv_size=2048
    else:
        recv_size+=all_size

    # 打印进度条
    percent = recv_size / total_size  # 1024 / 333333
    progress(percent)
模拟下载以及进度条
import sys

def copy_file():
    if (len(sys.argv)-1) == 2:
        src_file=sys.argv[1]
        des_file=sys.argv[2]
        with open(src_file,"rb") as f,\
            open(des_file,"w") as fw:
            for line in f:
                fw.write(des_file)
        print("拷贝完成")
    else:
        print("输入参数有误")
copy_file()
文件拷贝

 

posted @ 2020-03-30 14:39  耗油炒白菜  阅读(247)  评论(0编辑  收藏  举报