Skip to content

Instantly share code, notes, and snippets.

@CodeBoy2006
Last active March 19, 2020 14:20
Show Gist options
  • Select an option

  • Save CodeBoy2006/03c2ec523f3843abfdccea2be0ba5070 to your computer and use it in GitHub Desktop.

Select an option

Save CodeBoy2006/03c2ec523f3843abfdccea2be0ba5070 to your computer and use it in GitHub Desktop.
Python 获取根目录示例,可以用在目录混乱的时候统一路径
# 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