Python Software directory structure specification

Source: Internet
Author: User

Execution of files, configuration files, and program files by standard is not in a folder.

View the current path of the file,

1. Using a third-party platform: print __file__ output D:\soft\eclipse-workspace\ceshi\lianx1.py The output looks at the absolute path is actually a relative path.

The relative path can be seen directly in DOS mode:

2. Execute: Print Os.path.abspath (__file__) to output the absolute path of the current file

When executing file and program files are not in a folder, how to run the files under different folders by executing the execution file, you need: Os.path.dirname () method.

Os.path.dirname () takes the path of the current file, not the file name, with the ability to return to the previous level.

Import Osprint os.path.dirname (Os.path.abspath (__file__)) >>d:\soft\eclipse-workspace\ceshi

Then return to the upper level,

Import Osprint os.path.dirname (Os.path.dirname (Os.path.abspath (__file__))) >>d:\soft\eclipse-workspace

Examples, such as:

By executing the atm.py file to execute the program in the main.py file, you need to do the following in the atm.py file.

Import Osdir=os.path.dirname (Os.path.dirname (Os.path.abspath (__file__))) Sys.path.append (dir) from core import Mainmain.log ()

Python Software directory structure specification

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.