作业 —— day22

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

python3.8 run.py 文件夹
import os
import sys

address = sys.argv[1]
res = os.path.getsize(f'{address}')
print(res, '个字节')

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

随机验证码

def make_code_plus(size=6):  # 默认长度为6
    import random
    res = ''
    for i in range(size):
        s1 = chr(random.randint(65, 90))  # ASCII码表中,65-90是A-Z
        s2 = chr(random.randint(97, 122))  # ASCII码表中,97-122是a-z
        s3 = str(random.randint(0, 9))  # 随机整数0-9

        res += random.choice([s1, s2, s3])
    return res


print(make_code_plus())

# jnmp8L

模拟下载以及打印进度条

import time


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


recv_size = 0
total_size = 25600
while recv_size < total_size:
    time.sleep(0.2)
    recv_size += 1024
    percent = recv_size / total_size  # 1024 / 25600
    progress(percent)
    
#  [>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 100%

文件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('拷贝成功!')
    
#  python3 test.py D:\1.docx D:\2.docx   #拷贝成功
posted @ 2020-03-30 21:15  轻描丨淡写  阅读(250)  评论(0编辑  收藏  举报