Directory structure:
Package |------Bin | ------start.py|------conf |------settings.py|------Core |------ src.py |------db | ------table.db|------lib |------common.py|------log |--- ---transaction.log|------README
1. Writing the core code in SRC
fromLibImportCommondefshopping ():Print('Shopping')defPay ():Print() Common.logger ('xxxxxxxxxx')deftransfer ():Print()defWithdraw ():Print('Cash Withdrawal') Func_dic={ '1': Shopping,'2': Pay,'3': Transfer,'4': Withdraw} #让用户选择启动哪个方法defrun (): whileTrue:Print("""0 Exit 1 Shopping 2 pay 3 transfer 4 withdrawal""") Choice=input ('Please enter your action:'). Strip ()ifChoice = ='0': Break ifChoice not inchFunc_dic:Print("the input instruction does not exist, please re-enter") ContinueFunc_dic[choice] ()
2.bin/start starting the entry for the program
Os.path.dirname (Os.path.dirname (__file__)) ================> dynamic access to the root directory of the software to address migration issues
Import SYS Import OS from Import = Os.path.dirname (Os.path.dirname (__file__)) sys.path.append (base_dir)if __name__ ' __main__ ' : src.run ()
Set log path (and database path) in 3.conf/settings.py
The same is the dynamic access to the root directory of software, and then stitching to the specified directory of the software log to solve the migration problem
Import osbase_dir=os.path.dirname (os.path.dirname (__file__'log' ' Transaction.log ')
A general method of 4.lib/common.py
from Import Settings Import Time def Logger (msg): With open (settings. Transaction_log_path,'at', encoding='utf-8' As F: f.write ('%s%s\n' % (time.strftime ('%y-%m- %d%h:%m:%s'), msg))
Python Learning _ directory specification for software development and sample code (troubleshooting path issues with software porting)