录规范
项目目录规范
bin
存放执行文件,整个程序的入口文件,比如启动功能
conf
配置文件
lib
存放自定义的共享库,经常使用的一些功能
core
存放整个代码程序的核心逻辑
db
存放数据库相关的
log
日志相关的
Readme
记录整个项目的描述信息
项目文件规范
Pythonsrc.py def register(): print('注册。。。') def pay(): print('支付。。。') def transfer(): print('转账。。。') def withdraw(): print('提现。。。') func_dic = { '1': register, '2': pay, '3': transfer, '4': withdraw } # 核心功能,和用户交互 def run(): while True: print(""" 1 注册 2 支付 3 转账 4 提现 """) choice = input('>>>: ').strip() if choice in func_dic: func_dic[choice]() else: print('输入错误指令')
123456789101112131415161718192021222324252627282930313233343536 src . pydef register ( ) :print ( '注册。。。' )def pay ( ) :print ( '支付。。。' )def transfer ( ) :print ( '转账。。。' )def withdraw ( ) :print ( '提现。。。' )func_dic = {'1' : register ,'2' : pay ,'3' : transfer ,'4' : withdraw}# 核心功能,和用户交互def run ( ) :while True :print ( """1 注册2 支付3 转账4 提现""" )choice = input ( '>>>: ' ) . strip ( )if choice in func_dic :func_dic [ choice ] ( )else :print ( '输入错误指令' )
获取项目根路径
强调:只有被导入的模块才能使用
.
或..
的语法
编辑start.py
Pythonimport sys sys.path.append(r'E:\Project\core')
1234 import syssys . path . append ( r 'E:\Project\core' )这样写会有问题,在程序给别人使用时,别人的路径不可能和程序开发者的目录路径一样
改进
__file__
表示当前文件的绝对路径
abspath()
规范路径格式Pythonimport sys import os print(__file__) print(os.path.abspath(__file__)) # sys.path.append(r'E:\Project\core') # os.path.dirname() # 执行结果 E:/Project/bin/start.py E:\Project\bin\start.py
123456789101112 import sysimport osprint ( __file__ )print ( os.path . abspath ( __file__ ) )# sys.path.append(r'E:\Project\core')# os.path.dirname()# 执行结果E : / Project / bin / start . pyE : \ Project \ bin \ start . py
import sys import os print(os.path.dirname(os.path.abspath(__file__))) print(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # 执行结果 E:\Project\bin E:\Project import sys import os 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() # 执行结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import
sys
import
os
print
(
os.path
.
dirname
(
os.path
.
abspath
(
__file__
)
)
)
print
(
os.path
.
dirname
(
os.path
.
dirname
(
os.path
.
abspath
(
__file__
)
)
)
)
# 执行结果
E
:
\
Project
\
bin
E
:
\
Project
import
sys
import
os
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
(
)
# 执行结果
|
获取 db
目录文件的路径
编辑
setting.py
Pythonimport os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DB_PATH = '%s\%s\%s' % (BASE_DIR, 'db', 'db.txt') print(DB_PATH) # 执行结果 E:\Project\db\db.txt
12345678910 import osBASE_DIR = os.path . dirname ( os.path . dirname ( os.path . abspath ( __file__ ) ) )DB_PATH = '%s\%s\%s' % ( BASE_DIR , 'db' , 'db.txt' )print ( DB_PATH )# 执行结果E : \ Project \ db \ db . txt这种拼接方式有缺陷,如果是 Linux 系统,
\
右斜杠就不能用了
解决跨平台路径格式的问题
使用
os.path.join
Pythonimport os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DB_PATH = os.path.join(BASE_DIR, 'db', 'db.txt') print(DB_PATH) # 执行结果 E:\Project\db\db.txt
12345678910 import osBASE_DIR = os.path . dirname ( os.path . dirname ( os.path . abspath ( __file__ ) ) )DB_PATH = os.path . join ( BASE_DIR , 'db' , 'db.txt' )print ( DB_PATH )# 执行结果E : \ Project \ db \ db . txtLinux 系统输出格式
Pythonimport os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DB_PATH = os.path.join(BASE_DIR, 'db', 'db.txt') print(DB_PATH) # 执行结果 /data/Project/db/db.txt
12345678910 import osBASE_DIR = os.path . dirname ( os.path . dirname ( os.path . abspath ( __file__ ) ) )DB_PATH = os.path . join ( BASE_DIR , 'db' , 'db.txt' )print ( DB_PATH )# 执行结果/ data / Project / db / db . txt
获取 log
目录路径
编辑
settings.py
LOG_PATH = os.path.join(BASE_DIR, 'log', 'access.log') print(LOG_PATH) # 执行结果 E:\Project\log\access.log
1
2
3
4
5
6
7
|
LOG_PATH
=
os.path
.
join
(
BASE_DIR
,
'log'
,
'access.log'
)
print
(
LOG_PATH
)
# 执行结果
E
:
\
Project
\
log
\
access
.
log
|
项目目录规范示例
项目名/core/src.py
from conf import settings from lib import common def register(): print('注册。。。') uname = input('用户名:').strip() pwd = input('密码:').strip() with open(settings.DB_PATH, 'a', encoding='utf-8') as f: f.write('%s:%s\n' % (uname, pwd)) common.logger(uname) print('注册成功') def pay(): print('支付。。。') def transfer(): print('转账。。。') def withdraw(): print('提现。。。') func_dic = { '1': register, '2': pay, '3': transfer, '4': withdraw } # 核心功能,和用户交互 def run(): while True: print(""" 1 注册 2 支付 3 转账 4 提现 按 q 退出 """) choice = input('请选择相应的操作: ').strip() if choice == 'q': break if choice not in func_dic: print('输入错误指令,请重新输入') continue func_dic[choice]()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
from
conf
import
settings
from
lib
import
common
def
register
(
)
:
print
(
'注册。。。'
)
uname
=
input
(
'用户名:'
)
.
strip
(
)
pwd
=
input
(
'密码:'
)
.
strip
(
)
with
open
(
settings
.
DB_PATH
,
'a'
,
encoding
=
'utf-8'
)
as
f
:
f
.
write
(
'%s:%s\n'
%
(
uname
,
pwd
)
)
common
.
logger
(
uname
)
print
(
'注册成功'
)
def
pay
(
)
:
print
(
'支付。。。'
)
def
transfer
(
)
:
print
(
'转账。。。'
)
def
withdraw
(
)
:
print
(
'提现。。。'
)
func_dic
=
{
'1'
:
register
,
'2'
:
pay
,
'3'
:
transfer
,
'4'
:
withdraw
}
# 核心功能,和用户交互
def
run
(
)
:
while
True
:
print
(
"""
1 注册
2 支付
3 转账
4 提现
按 q 退出
"""
)
choice
=
input
(
'请选择相应的操作: '
)
.
strip
(
)
if
choice
==
'q'
:
break
if
choice
not
in
func_dic
:
print
(
'输入错误指令,请重新输入'
)
continue
func_dic
[
choice
]
(
)
|
项目名/conf/settings.py
import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DB_PATH = os.path.join(BASE_DIR, 'db', 'db.txt') LOG_PATH = os.path.join(BASE_DIR, 'log', 'access.log')
1
2
3
4
5
6
|
import
os
BASE_DIR
=
os.path
.
dirname
(
os.path
.
dirname
(
os.path
.
abspath
(
__file__
)
)
)
DB_PATH
=
os.path
.
join
(
BASE_DIR
,
'db'
,
'db.txt'
)
LOG_PATH
=
os.path
.
join
(
BASE_DIR
,
'log'
,
'access.log'
)
|
项目名/lib/common.py
from conf import settings import time # print(time.strftime('%Y-%m-%d %X')) def logger(msg): with open(settings.LOG_PATH, 'a', encoding='utf-8') as f: f.write('%s, %s, 注册成功\n' % (time.strftime('%Y-%m-%d %X', time.localtime()), msg))
1
2
3
4
5
6
7
8
9
|
from
conf
import
settings
import
time
# print(time.strftime('%Y-%m-%d %X'))
def
logger
(
msg
)
:
with
open
(
settings
.
LOG_PATH
,
'a'
,
encoding
=
'utf-8'
)
as
f
:
f
.
write
(
'%s, %s, 注册成功\n'
%
(
time
.
strftime
(
'%Y-%m-%d %X'
,
time
.
localtime
(
)
)
,
msg
)
)
|
项目名/bin/start.py
import sys import os 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()
1
2
3
4
5
6
7
8
9
10
11
|
import
sys
import
os
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
(
)
|
运行 start.py
cat db.txt egon:123 tail access.log 2018-09-24 01:08:11, egon, 注册成功
1
2
3
4
5
6
|
cat
db
.
txt
egon
:
123
tail
access
.
log
2018
-
09
-
24
01
:
08
:
11
,
egon
,
注册成功
|