1. 检索文件夹大小的程序
# 要求执行方式如下
# python3.8 run.py 文件夹
import os
import sys
def get_file_size(files_path, size=0):
for file in os.listdir(files_path):
file = os.path.join(files_path, file)
if not os.path.isfile(file):
files_path = file
if len(os.listdir(files_path)) == 0:
size = 0
else:
return get_file_size(files_path, size),
else:
size += os.path.getsize(os.path.join(files_path, file))
return print(size,'个字节')
files_path1 = sys.argv[1]
get_file_size(files_path1)
2. 随机验证码程序
import random
def func():
addition = ''
for i in range(6):
a = chr(random.randint(ord('a'), ord('z')))
b = chr(random.randint(ord('A'), ord('Z')))
c = str(random.randint(0, 9))
res = random.choice([a, b, c])
addition += res
return print(addition)
func()
3. 拟下载以及打印进度条
import time
def func(file_load, down_load=0):
def progress_bar(percent):
bar = int(percent * 50) * '#'
print('
[%-50s] %d%%' % (bar, percent * 100), end='')
while down_load < file_load:
percent = down_load / file_load
down_load += 1024
time.sleep(0.3)
if percent > 1:
percent = 1
progress_bar(percent)
func(33333)
4. 文件copy脚本
import sys
ori_file = sys.argv[1]
cop_file = sys.argv[2]
with open(f'%s' % ori_file, mode='rb') as f_read,
open(f'%s' % cop_file, mode='wb') as f_write:
for line in f_read:
f_write.write(line)
print('复制成功')
import hashlib
def register():
while True:
username = input('请输入用户名:').strip()
with open(r'db.txt', mode='r+b') as f1:
for line in f1:
line = line.strip().split(':'.encode('utf-8'))
if username.encode('utf-8') == line[0]:
print('用户已存在,请重新输入:')
break
else:
password1 = input('请输入用密码:').strip()
password2 = input('请再次输入用密码:').strip()
if password1 == password2:
m1 = hashlib.sha256()
m1.update(password1.encode('utf-8'))
f1.seek(0, 2)
f1.write('
{}:{}'.format(username, m1.hexdigest()).encode('utf-8'))
break
else:
print('两次密码不一致,请重新输入:')
def login():
while True:
username = input('请输入账号:').strip()
password = input('请输入密码:').strip()
with open(r'db.txt', 'rb') as f2:
for line in f2:
line = line.strip().split(':'.encode('utf-8'))
if username.encode('utf-8') == line[0]:
m2 = hashlib.sha256()
m2.update(password.encode('utf-8'))
if m2.hexdigest().encode('utf-8') == line[1]:
print('登录成功')
return username
else:
print('密码输入错误,请重新输入:')
break
else:
print('账号不存在,请重新输入:')
register()
login()
import hashlib
def download_file():
with open(r'download.txt', 'rb') as f1:
m1 = hashlib.sha256()
for i in [0, 100, 200]:
f1.seek(i, 0)
res1 = f1.read(50)
m1.update(res1)
info1 = m1.hexdigest()
return info1
def upload_file():
with open(r'upload.txt', 'rb') as f2:
m2 = hashlib.sha256()
for j in [0, 100, 200]:
f2.seek(j, 0)
res2 = f2.read(50)
m2.update(res2)
info2 = m2.hexdigest()
return info2
if download_file() == upload_file():
print('文件完整')
else:
print('文件缺失')
import json
import hashlib
import os
def password_sha256(password):
m1 = hashlib.sha256()
m1.update('天王盖地虎'.encode('utf-8'))
m1.update(password.encode('utf-8'))
m1.update('宝塔镇河妖'.encode('utf-8'))
return m1.hexdigest()
def register():
while True:
username = input('请输入用户名:').strip()
with open(r'bd.json', mode='rt', encoding='utf-8') as f1,
open(r'bd_swap.json', mode='wt', encoding='utf-8') as f2:
json_dic = json.load(f1)
if username in json_dic:
print('用户已存在,请重新输入:')
continue
else:
password1 = input('请输入用密码:').strip()
password2 = input('请再次输入用密码:').strip()
if password1 == password2:
psd = password_sha256(password1)
json_dic[username] = {
'password': psd,
'ID': 'junior'
}
json.dump(json_dic, f2,indent=1)
break
else:
print('两次密码不一致,请重新输入:')
os.remove(r'bd.json')
os.rename(r'bd_swap.json', r'bd.json')
def login():
while True:
username = input('请输入账号:').strip()
password = input('请输入密码:').strip()
with open(r'bd.json', 'rt', encoding='utf-8') as f3:
user_dic = json.load(f3)
if username in user_dic:
if password_sha256(password) == user_dic[username]['password']:
print('登录成功')
return username
else:
print('密码输入错误,请重新输入:')
continue
else:
print('账号不存在,请重新输入:')
register()
login()