今日作业:
1、检索文件夹大小的程序
要求执行方式如下python3.8 run.py 文件夹
import os
import sys
file_list = os.listdir(sys.argv[1])
def file_size(file_list,size = 0):
for file in file_list:
if not os.path.isfile(file):
file_list = os.listdir(file)
if not file_list:
return file_size(file_list,size)
else:
continue
else:
size += os.path.getsize(file)
file_size(file_list)
2、明天上午日考:随机验证码、模拟下载以及打印进度条、文件copy脚本
1、随机验证码
import random
def make_code(size=4):
res=''
for i in range(size):
s1=chr(random.randint(65,90))
s2=str(random.randint(0,9))
s3=chr(random.randint(97,122))
print(res)
return res
print(make_code(6))
2、模拟下载以及打印进度条
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 = 33333
while recv_size < total_size:
time.sleep(0.2)
recv_size += 1024
percent = recv_size / total_size
progress(percent)
3、文件copy脚本
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)