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