检索文件大小,验证码生成,下载条,copy文件
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脚本
随机验证码
import random
def check_num():
res = ''
for i in range(6):
src1 = random.randint(0,9)
src2 = random.randint(65,90)
src3 = random.randint(97,122)
src2 = random.choice([src2,src3])
src2 = chr(src2)
num = random.choice([src1,src2])
res += str(num)
return res
print(check_num())
模拟下载以及打印进度条
def down_l():
import time
size = 200000
size_per = 0
size_l = size
while size_l > 0:
size_per += 1024
percent = size_per / size
if size_l < 1024:
percent = 1
res = '#'*(int((percent*10)))
print('\r[%-10s] %s%%'%(res,int(percent*100)),end='')
size_l = size - size_per
time.sleep(0.05)
down_l()
import os
os.system('dir')
文件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])