1、检索文件夹大小的程序,要求执行方式如下
python3.8 run.py 文件夹
import os
import sys
def get_file_size(files_list, size=0):
for file in files_list:
if not os.path.isfile(file):
files_list = os.listdir(file)
if len(files_list) == 0:
size += 0
else:
return get_file_size(files_list, size)
else:
size += os.path.getsize(file)
return print(size)
file_dir = sys.argv[1]
file_list = os.listdir(file_dir)
get_file_size(file_list)
2、
随机验证码
import random
def make_code(size=4):
res=''
for i in range(size):
s1=chr(random.randint(65,90))
s2=str(random.randint(0,9))
res+=random.choice([s1,s2])
return res
print(make_code(6))
模拟下载以及打印进度条
import time
def progress(percent):
if percent > 1:
percent = 1
res = int(50 * percent) * '#'
print('
[%-50s] %d%%' % (res, int(100 * percent)), end='')
recv_size=0
total_size=1025011
while recv_size < total_size:
time.sleep(0.01) # 下载了1024个字节的数据
recv_size+=1024 # recv_size=2048
# 打印进度条
print(recv_size)
percent = recv_size / total_size # 1024 / 333333
progress(percent)
文件copy脚本
src_file = sys.argv[1]
dst_file = sys.argv[2]
with open(r'%s'%src_file, mode='rb') as read_f,
open(r'%s'%dst_file, mode='wb') as write_f:
for line in read_f:
write_f.write(line)