# 今日作业:
# 必做题:
# 1.检索文件夹大小的程序,要求执行方式如下
# python3.8 run.py 文件夹
#os.path.getsize 查看文件大小
import os
res = os.path.getsize(r'D:python oldboyday22常用模块的使用
un.py')
print(res)
# 2.明天上午日考:随机验证码、模拟下载以及打印进度条、文件copy脚本
# 1.验证码:
# 普通版:4位(大写字母+数字)
def make_code(size=4): # 默认长度为4
import random
res = ''
for i in range(size):
s1 = chr(random.randint(65,90)) # ASCII码表中,65-90是A-Z
s2 = str(random.randint(0,9)) # 随机整数0-9
res += random.choice([s1,s2])
return res
print(make_code())
# 升级版:6位(+小写字母+大写字母+数字)
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())
# 2.模拟下载以及打印进度条
import time
def progress(percent):
if percent > 1:
percent = 1
res = int(50 * percent) * '#'
print('
[%-50s] %d%%' % (res, percent * 100), end='')
recv_size = 0
total_size = 37215
while recv_size < total_size:
time.sleep(0.2)
recv_size += 1024
percent = recv_size / total_size # 1024 / 25600
progress(percent)
#3.文件copy脚本
# 1.sys模块
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)
# # 在run.py所在的文件夹下,按住shift,右键,选择“在此处打开power shell”,输入
# # 格式:python3 run.py 原文件路径 新文件路径
# # python3 run.py D:1.docx D:2.docx #拷贝成功