# 1、检索文件夹大小的程序,要求执行方式如下
# python3.8 run.py 文件夹
import os
path=sys.argv[1]
size= 0
for line in os.listdir(path):
path2 = os.path.join(path, line)
if os.path.isfile(path2):
size+=os.path.getsize(path2)
elif os.path.isdir(path2):
size+=os.path.getsize(path2)
print(size)
明天上午日考:随机验证码、模拟下载以及打印进度条、文件copy脚本
随机验证码:
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))
res+=random.choice([s1,s2])
return res
print(make_code(6))
打印进度条
import sys
import time
def progress(percent,width=50):
if percent>1:
percent=1
show_str = ('[%%-%ds]'%width)%(int(percent*width)*'*')
print('
%s %s%%'%(show_str,int(percent*100)),end='')
data_size=10250
recv_size=0
while recv_size < data_size:
time.sleep(0.1) #模拟数据的传输延迟
recv_size+=1024 #每次收1024
percent=recv_size/data_size #接收的比例
progress(percent,width=70) #进度条的宽度70
文件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)