Python Learning _ Directory specification for software development and sample code (to solve the path problem of software porting)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.