检索文件大小,验证码生成,下载条,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])
posted @   pythoner_wl  阅读(177)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示