Python 软件开发的目录规范

录规范

项目目录规范

bin 存放执行文件,整个程序的入口文件,比如启动功能
conf 配置文件
lib 存放自定义的共享库,经常使用的一些功能
core 存放整个代码程序的核心逻辑
db 存放数据库相关的
log 日志相关的
Readme 记录整个项目的描述信息

项目文件规范

Python
src.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('输入错误指令')
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
src . 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 ( '输入错误指令' )
 

获取项目根路径

强调:只有被导入的模块才能使用 ... 的语法
编辑 start.py

Python
import sys sys.path.append(r'E:\Project\core')
1
2
3
4
import sys
 
sys . path . append ( r 'E:\Project\core' )
 

这样写会有问题,在程序给别人使用时,别人的路径不可能和程序开发者的目录路径一样

改进

__file__ 表示当前文件的绝对路径
abspath() 规范路径格式

Python
import 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
1
2
3
4
5
6
7
8
9
10
11
12
import 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
 

Python
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

Python
import 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
1
2
3
4
5
6
7
8
9
10
import 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
 

这种拼接方式有缺陷,如果是 Linux 系统,\ 右斜杠就不能用了

解决跨平台路径格式的问题

使用 os.path.join

Python
import 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
1
2
3
4
5
6
7
8
9
10
import 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
 

Linux 系统输出格式

Python
import 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
1
2
3
4
5
6
7
8
9
10
import 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
 

获取 log 目录路径

编辑 settings.py

Python
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

Python
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

Python
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

Python
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

Python
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

Python
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 , 注册成功
 



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值