start.py
# import sys,os
# #os.path.abspath(__file__) # 取当前路径的绝对路径
#
#
# print(os.path.abspath(__file__))
# # /Users/sg/PycharmProjects/py-study/ATM/bin/start.py
#
# print(os.path.dirname(os.path.abspath(__file__)))
# # /Users/sg/PycharmProjects/py-study/ATM/bin
# print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# # /Users/sg/PycharmProjects/py-study/ATM
# 定义ATM的路径作为起始路径,方便各个模块之间的变量导入
import os,sys
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from core import src
if __name__ == '__main__':
src.run()
setting.py
# 定义各个模块的路径
import os
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# 以上路径为/Users/sg/PycharmProjects/py-study/ATM
LOG_PATH=os.path.join(BASE_DIR,'log','access.log')
DB_PATH=os.path.join(BASE_DIR,'db','user')
或
DB_PATH=r'%s/db/db.txt' %BASE_DIR
LOG_PATH=r'%s/log/user.log' %BASE_DIR
# print(LOG_PATH)
# # /Users/sg/PycharmProjects/py-study/ATM/log/access.log
#
# print(DB_PATH)
# # /Users/sg/PycharmProjects/py-study/ATM/db/user
src.py
from lib import common
def shop():
print('购物。。。')
def check_balance():
print('查看余额。。。')
def transfer_accounts():
print('转账。。。')
log_msg='转账了一个亿' #定义了日志输出
common.logger(log_msg)
def run():
msg='''
1 购物
2 查看余额
3 转账
'''
while True:
print(msg)
choice=input('>>:').strip()
if not choice:continue
if choice == '1':
shop()
elif choice == '2':
check_balance()
elif choice == '3':
transfer_accounts()
另一参考版本---------------------------------
from conf import setting
# from lib import common
#
# def login():
# count=0
# while True:
# name=input('name:').strip()
# password=input('password:').strip()
# res=common.select(name)
# if name==res[0] and password==res[1]:
# print('%s 登录成功' %name)
# else:
# print('用户名或密码输入错误')
# count+=1
# if count == 3:
# print('输入错误次数过多!')
# break
#
#
#
# def register():
# name = input('name:').strip()
# password = input('password:').strip()
# with open(setting.DB_PATH,'a',encoding='utf-8') as f:
# f.write('%s:%s\n' %(name,password))
# common.logger('%s 注册成功\n' %name)
# print('注册成功')
#
#
# def shopping():
# pass
#
# def pay():
# pass
#
# def transfer():
# pass
#
# func_dir={
# '1':login,
# '2':register,
# '3':shopping,
# '4':pay,
# '5':transfer
# }
#
#
# def run():
# while True:
# print('''
# 1 登录
# 2 注册
# 3 购物
# 4 付款
# 5 转账
# 6 退出
# ''')
# choice=input('输入编号:').strip()
# if choice == '6':break
# if choice not in func_dir:
# print('非法输入')
# continue
# func_dir[choice]()
common.py
# 定义日志输出文件路径
from conf import settings
def logger(msg):
with open(settings.LOG_PATH,'a',encoding='utf-8') as f:
f.write('%s\n' %msg)
参考另一版本------------
# import time
# from conf import setting
#
#
# def logger(msg):
# current_time=time.strftime('%Y-%m-%d %X')
# with open(setting.LOG_PATH,'a',encoding='utf-8') as f:
# f.write('%s %s' %(current_time,msg))
#
# def select(name):
# with open(setting.DB_PATH, 'r', encoding='utf-8') as f:
# for line in f:
# info=line.strip('\n').split(':')
# return info
执行start.py验证
/usr/local/bin/python3 /Users/shiheng/PycharmProjects/py-study/ATM/bin/start.py
1 购物
2 查看余额
3 转账
>>:3
转账。。。
1 购物
2 查看余额
3 转账
查看access.log内容为:
转账了一个亿