1、检索文件夹大小的程序,要求执行方式如下
python3.8 run.py 文件夹
'''
c.txt
hahaha
hahaha
hahaha
'''
'''run.py'''
import os
os.system('python3 /Users/tophan/2020_python/day22/sys.argv应用.py /Users/tophan/2020_python/day22/c.txt')
'''sys.argv应用.py'''
import sys
import os
res = sys.argv[1]
if os.path.exists(res):
size = os.path.getsize(res)
print(f'文件大小为{size}') #文件大小为21
2、随机验证码
import random
def make_code(size_num):
'''
验证功能
size_num:验证码字符个数
'''
str1 = '' #验证码
for i in range(size_num):
#大写字母
str_upper = chr(random.randint(65, 90)) # A-Z
#小写字母
str_lower = chr(random.randint(97, 122)) # a-z
#数字
num = str(random.randint(0, 9)) #0-9
res = random.choice((str_lower, str_upper, num))
str1 += res
return str1
res = make_code(6)
3、进度条
import time
def progress(percent):
res_str = '#' * int(percent * 50)
time.sleep(0.01)
print('
[%- 50s] %d%%' % (res_str, int(100 * percent)), end='')
total_size = 333333
recv_size = 0
while recv_size < total_size:
# 未接收字的节数
remaining_size = total_size - recv_size
if remaining_size >= 1024:
# 每次接收的字节数
recv_size += 1024
# 获取接收的数据占总数据多少百分比
else:
recv_size += remaining_size
percent = recv_size / total_size
progress(percent)