1 统计文件夹大小
def get_filesize(file_path):
count = 0
res=None
files_sum = os.listdir(file_path)
for files in files_sum:
new_files = os.path.join(file_path,files)
if os.path.isdir(new_files):
res= get_filesize(new_files)
elif os.path.isfile(new_files):
size = os.path.getsize(new_files)
count+=size
if res==None:
return count
elif res>0:
return count+res
res = get_filesize(path)
print(res)
2 模拟下载进度条
def progress(percent):
if percent>1:
percent=1
str_sap = int(percent*50)*'#'
print(("
[%-50s] %s%%")%(str_sap,int(percent*100)),end="")
recv_size = 0
total_size = 1024
while recv_size<total_size:
recv_size+=10
import time
time.sleep(0.1)
percent = recv_size/total_size
progress(percent)
3 copy
import sys
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)
4 验证码
import random
def make_code(size):
str0 = ""
for i in range(size):
str1 = chr(random.randint(65,90))
str2 = chr(random.randint(97,122))
num = str(random.randint(0,9))
str0 += random.choice([str1,str2,num])
return str0
print(make_code(10))