3-30作业
# 1、检索文件夹大小的程序,要求执行方式如下python3.8 run.py文件夹
import sys,os
src = sys.argv[1]
res = 0
def size_of_file(file):
global res
for file1 in os.listdir(r'%s'%file):
path = os.path.join(file,file1)
print(path)
if os.path.isfile(path):
res += os.path.getsize(src)
else:
size_of_file(path)
if os.path.isfile(r'%s'%src):
res = os.path.getsize(src)
else:
size_of_file(src)
print(res)
# 2、明天上午日考:随机验证码、模拟下载以及打印进度条、文件copy脚本
(一)随机验证码
1 import random
2
3 def make_code(size):
4 res=''
5 for x in range(size):
6 l1=chr(random.randint(65,90))
7 l2=str(random.randint(0,9))
8 res+=random.choice([l1,l2])
9 return res
10 print(make_code(8))
(二)模拟下载及打印进度条
import time def progress(percent): if percent>1: percent=1 res = int(66 * percent) * '$' print('\r[%-50s] %d%%' % (res, int(100 * percent)), end='') recv_size=0 data_size=102411 while recv_size < data_size: time.sleep(0.08) #模拟数据的传输延迟时间 recv_size+=1024 #下载了1024个字节的数据 percent=recv_size/data_size #接收的比例 progress(percent)
(三)文件copy脚本
import sys
import os
file_path = sys.argv
def copy(src, dst):
name = os.path.basename(src)
print(name)
if os.path.isfile(dst):
print('目标路径不是个文件夹')
return None
dst = os.path.join(dst,name)
with open(src,'rb') as f1,\
open(dst,'wb') as f2:
f2.write(f1.read())
copy(file_path[1],file_path[2])