When using Python, you will always encounter the use of path switching, such as the test.py under folder test to invoke the Data.txt file under the Data folder:
.
└──folder
├──data
│ └──data.txt
└──test
└──test.py
A method can be added to the data file __init__.py then import data in the test.py can invoke Data.txt file;
Another way to manipulate the directory structure with the help of the Python OS module is to use the following:
Import OS
print ' * * * Get current directory * * *
os.getcwd ()
print Os.path.abspath (Os.path.dirname (__file__))
print ' * * * get Superior Directory * * * *
Os.path.abspath (Os.path.dirname (Os.path.dirname (__file__))
Print Os.path.abspath (Os.path.dirname (OS.GETCWD))
print Os.path.abspath (Os.path.join (), "..."
) print ' * * * gets on top of the directory * * *
Os.path.abspath (Os.path.join (OS.GETCWD), ". /.."))
The output results are:
Get current Directory * * */workspace/demo/folder/test/workspace/demo/folder/test * * *
access to
Superior directory * * *
Workspace/demo/folder
/workspace/demo/folder
/workspace/demo/folder
* * * access to Superior directory * * * *
Workspace/demo