一:主要是是os模板练习案例。
import os
import datetime
import time
DB_PATH = 'db'
def history(user_path):
if not os.path.exists(user_path):
print('无历史记录')
return
with open(user_path, mode='r', encoding='utf-8') as f:
for line in f:
line = line.strip()
print(line)
def booking(user_path):
while True:
location = input('请输入景区,或者输入q/Q,退出订票:')
if location.upper().strip() == "Q":
break
book_num = input('输入订几张:')
ctime_string = datetime.datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
book_string = '{},{},{}
'.format(location, book_num, ctime_string)
with open(user_path, mode='a', encoding='utf-8') as f:
f.write(book_string)
def run():
if not os.path.exists(DB_PATH):
os.makedirs(DB_PATH)
# 2.
user_name = input('请输入用户名:')
user_path = os.path.join(DB_PATH, '{}.txt'.format(user_name))
if os.path.exists(user_path):
print('{}用户已经存在'.format(user_name))
else:
print('{}用户是新用户'.format(user_name))
# 3.
func_dict = {'1': history, '2': booking}
while True:
print('1:是查看历史订单,2:预定')
choice = input('请选择功能,或者退出q/Q:')
if choice.upper().strip() == 'Q':
return
func = func_dict.get(choice.strip())
if not func:
print('序号输入错误')
continue
func(user_path)
run()
二:账号注册
import os
import sys
import hashlib
import json
import datetime
DATA_FILE = "user_info.txt"
SALT_STR = "dgd5"
def md5(*args):
salt_str, user_pwd = args
hashlib_md5 = hashlib.md5(salt_str.encode('utf-8'))
hashlib_md5.update(user_pwd.encode('utf-8'))
pwd_str = hashlib_md5.hexdigest()
return pwd_str
def run():
while True:
flag = 0
user_name = input('请输入注册的用户:')
if os.path.isfile(DATA_FILE):
with open(DATA_FILE, mode='r', encoding='utf-8') as file:
for line in file:
if user_name in line:
print('这个{}用户已经被注册,请重新注册其它账号'.format(user_name))
flag = 1
break
if flag:
continue
user_pwd = input('请输入用户的密码:')
user_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
pwd_str = md5(SALT_STR, user_pwd)
with open(DATA_FILE, mode='a', encoding='utf-8') as file:
mesg = "{},{},{}".format(user_name.strip(), pwd_str.strip(), user_time.strip())
file.write(mesg)
file.write('
')
run()
三:时间有限,后面会补充os目录其它功能