zoukankan      html  css  js  c++  java
  • 作业3月30号

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

    import os
    import sys
    
    
    def get_size(dir):
        items = os.listdir(dir)
        for item in items:
            item = os.path.join(dir, item)
            if os.path.isdir(item):
                get_size(item)
    
            file_size = os.path.getsize(item)
            items_size.append(file_size)
    
    
    if not sys.argv:
        print('文件夹不存在')
        sys.exit()
    
    items_size = []
    get_size(sys.argv[1])
    print(sum(items_size))


    2、随机验证码

    def func(n=4):
        res=''
        for line in range(n):
            s1=chr(random.randint(65,90))
            s2=chr(random.randint(97,122))
            s3=str(random.randint(0,9))
            res+=random.choice([s1,s2,s3])
        return res
    print(func())

    3、模拟下载以及打印进度条

    import time
    
    
    def progress(percent):
        if percent > 1:
            percent = 1
        res = int(50 * percent) * '#'
        print('
    [%-50s] %d%%' % (res, int(100 * percent)), end='')
    
    recv_size=0
    total_size=1025011
    
    while recv_size < total_size:
        time.sleep(0.01) 
        recv_size+=1024 
    
        percent = recv_size / total_size  # 1024 / 333333
        progress(percent)

    4、文件copy脚本

    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)
    
    print('拷贝完成')
  • 相关阅读:
    Python 面向对象编程
    snmp获取交换机端口和对应ip
    python IPy
    Django F()与Q()函数
    装饰器使用
    log日志信息查看
    shell简单入门
    gunicorn开启、关闭和重启
    CF1453B
    ACWing845 八数码(BFS,全排列hash)
  • 原文地址:https://www.cnblogs.com/jingpeng/p/12601629.html
Copyright © 2011-2022 走看看