软件开的目录规范+sys,os,time模块
——————————————————————————————————————————————————————————————————
start.py
import sys,os
# print(__file__)#当前文件的一个路径
# print(os.path.abspath(__file__)) #当前文件的绝对路径
# print(os.path.dirname(os.path.abspath(__file__)))
# print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
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()
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
src.py
def login():
print('登陆功能')
def register():
print('注册')
name=input('enter your name>>: ').strip()
pwd=input('enter you password>>: ').strip()
with open(settings.DB_PATH,mode='at',encoding = 'utf-8')as f:
f.write('%s:%s \n'%(name,pwd))
f.flush()
common.logger('%s注册成功 \n'%name)
print('注册成功')
def shopping():
print('购物')
def transfer():
print('转账')
def pay():
print('支付')
funcs_dic={
'1':login,
'2':register,
'3':shopping,
'4':transfer,
'5':pay
}
def run():
while True:
print("""
1.登陆 | 2.注册 | 3.购物 | 4.转账 | 5.支付 | 6.退出
""")
choice=input('>>>: ').strip()
if choice == '6':return
if choice not in funcs_dic:
print('错误,请再次输入')
continue
funcs_dic[choice]()
run()
_____________________________________________________________________________________________________________________________
settings.py
import os
# print(__file__)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DB_PATH = os.path.join(BASE_DIR,'db','db.txt') #DB_PATH = r'%s\db\db.txt'%BASE_DIR
LOG_PATH = os.path.join(BASE_DIR,'log','user.log.txt')
# print(DB_PATH)
# print(LOG_PATH)
# LOG_PATH = r'%s\log\user.log.txt'%BASE_DIR
# DB_PATH = r'D:\zrg\day19\文件开发的目录规范\ATM\db\db.txt'
#
# LOG_PATH = r'D:\zrg\day19\文件开发的目录规范\ATM\log\user.log.txt'
____________________________________________________________________________________________________________________________________
common.py
import time
from conf import settings
def logger(msg):
current_time=time.strftime('%Y-%m-%d %X')
with open(settings.LOG_PATH,'a',encoding='utf-8')as f:
f.write('%s %s'%(current_time,msg))
f.flush()
# print(time.strftime('%Y-%m-%d %X'))
___________________________________________________________________________________________________________________________
time.py
# 与时间相关的一个模块
import time
# 时间分三类:
# 1.时间戳 从1970年1.1日到现在的秒数
# print(time.time()/60/60/24)
# 计算时差
# t=time.time()
# time.sleep(2)
# t1=time.time()
# print(t1-t)
# 2.结构化时间
# print(time.localtime())
# 返回的是一个值,其中包含一个元组,里面是每个时间的值(东八区)
#time.struct_time(tm_year=2018, tm_mon=10, tm_mday=15, tm_hour=17, tm_min=29, tm_sec=7, tm_wday=0, tm_yday=288, tm_isdst=0)
# 单独获取某个时间属性
# print(time.localtime().tm_year)
# # 2018
# print(time.localtime().tm_hour)
# # 17
# 世界同一时间,比北京时间晚了八小时
# print(time.gmtime())
# 3.格式化的字符串时间
# print(time.strftime('%Y-%m-%d %H:%M:%S'))
# print(time.strftime('%Y-%m-%d %X'))
# 相互转换
# 时间戳转换成结构化
# print(time.localtime(0)) #元年
# 结构化转换成格式化的字符串
# 字符串转换为结构化
# 结构化转为时间戳
# 格林威治时间
time.asctime()
time.ctime()
____________________________________________________________________________________________________________________________
date.time.py
# 这是一个包,里面包含对事件的处理,对日期的处理
import datetime
#获取当前详细的时间
print(datetime.datetime.now())
# 获取当前时间的一部分
print(datetime.datetime.now().year)
# 替换时间的某一部分
# t1=datetime.datetime.now()
# t1=t1.replace(year=2020)
# print(t1)
# t1=datetime.datetime.now()
# t2=datetime.datetime.now()
# print(t2-t1)
时间差对象
tal=datetime.timedelta(days=20)
t1=datetime.datetime.now()
print(t1+tal)