作业19
1、检索文件夹大小的程序,要求执行方式如下
python3.8 run.py 文件夹
import os
import sys
file = sys.argv[1]
num = 0
def get_size(name):
global num
if os.path.isfile(name):
num += os.path.getsize(name)
elif os.path.isdir(name):
abs = os.path.abspath(name)
for i in os.listdir(abs):
i = os.path.join(abs,i)
get_size(i)
get_size(file)
print(num)
2、明天上午日考:随机验证码、模拟下载以及打印进度条、文件copy脚本
# --------------------------验证码--------------------------
import random
def code_maker(size=4):
code = ""
for i in range(size):
s1 = str(random.randint(0, 9))
s2 = chr(random.randint(65, 90))
code += random.choice([s1, s2])
return code
print(code_maker(6))
# --------------------------进度条--------------------------
import time
def pro(rate):
if rate > 1:
rate = 1
percent = int(100 * rate)
res = "*" * int(percent / 2)
time.sleep(0.2)
print("\r[{:<50}]{}%".format(res, percent), end="")
file_size = 55555
speed = 1024
down = 0
while down <= file_size:
time.sleep(0.2)
down += speed
pro(down / file_size)
# --------------------------copy--------------------------
import sys
file1 = sys.argv[1]
file2 = sys.argv[2]
def copyer(file1,file2):
with open(file1, "rb") as f1, open(file2, "wb") as f2:
res = f1.read()
f2.write(res)
copyer(file1,file2)