Last active
March 19, 2020 14:20
-
-
Save CodeBoy2006/03c2ec523f3843abfdccea2be0ba5070 to your computer and use it in GitHub Desktop.
Python 获取根目录示例,可以用在目录混乱的时候统一路径
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Python 获取根目录 | |
| #Windows | |
| import os | |
| curPath = os.path.abspath(os.path.dirname(__file__)) | |
| rootPath = curPath[:curPath.find("myProject\\")+len("myProject\\")] # 获取myProject,也就是项目的根路径 | |
| dataPath = os.path.abspath(rootPath + 'data\\train.csv') # 获取tran.csv文件的路径 | |
| #Mac | |
| import os | |
| curPath = os.path.abspath(os.path.dirname(__file__)) | |
| rootPath = curPath[:curPath.find("myProject/")+len("myProject/")] # 获取myProject,也就是项目的根路径 | |
| dataPath = os.path.abspath(rootPath + 'data/train.csv') # 获取tran.csv文件的路径 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment