例如,我在静态文件夹中有静态文件,文件从根目录的相对路径是
/static
根路径是 D:\proj\src
如果我想加载 xml 文件,我应该只给出相对路径,例如
/static/xml/a.xml.
我不需要更改字符串,添加 '../' 或 '../../' 取决于我所在的模块,这很愚蠢,所以你能不能给我一个更好的方法只使用标准自由?
项目 abs 根路径 + 相对路径应该没问题,但我尝试过但失败了。
如果您有文件名的完整路径,例如 /static/xml/a.xml
然后你可以像这样在 python 中很容易地到达它的不同部分
import os
fullFilePathName = '/static/xml/a.xml'
#
# the filename
#
theFilename = os.path.basename(fullFilePathName)
#
# The path to the file
#
thePath = os.path.dirname(fullFilePathName)
#
# Join the two together to form a path
#
fullName = os.path.join(thePath, theFileName)