检索文件夹大小的程序,要求执行方式如下 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)
随机验证码 def random_code(n=6): import random code = '' for i in range(n): s1 = chr(random.randint(97, 122)) # 小写字母ASCII码——>97-122 s2 = chr(random.randint(65, 90)) # 大写字母ASCII码——>65-90 s3 = str(random.randint(0, 9)) code += random.choice([s1, s2, s3]) return code print(random_code() ) 模拟下载以及打印进度条 import time def progress(file_size, download_size=0): def wrapper(percent): if percent > 1: percent = 1 bar = '#' * int(percent * 80) print(' [%-80s] %d%%' % (bar, int(percent * 100)), end='') while download_size <= file_size: time.sleep(0.05) download_size += 1024 percent = download_size / file_size wrapper(percent) progress(222222) 文件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])