对于os模块的os.path.abspath()获取文件绝对路径函数的一个小问题 | python | python 技术论坛-江南app体育官方入口
import os
the_gmae_file = os.path.abspath('aircraft_war.py')
print(the_gmae_file)
输出结果是:
这个已经困扰我很久了
求指点
import os
the_gmae_file = os.path.abspath('aircraft_war.py')
print(the_gmae_file)
输出结果是:
这个已经困扰我很久了
求指点
so what the question is ?
os.path.abspath(path)
return a normalized absolutized version of the pathname path. on most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)).
c:\>type d:\test5.py
import os
path = 'hello.py'
print(os.path.abspath(path))
print(os.path.normpath(os.path.join(os.getcwd(), path)))
c:\>python d:\test5.py
c:\hello.py
c:\hello.py
c:\>d:
d:\>python d:\test5.py
d:\hello.py
d:\hello.py
官方文档有写,如同一楼的答案,abspath
返回的是 os.path.join(os.getcwd(), path)
的路径。也就是你执行 python xxx.py
的所在路径与你传给 abspath
的参数拼接起来之后的路径。
我没理解错的话,os.path.abspath是从磁盘根路径到当前执行文件的路径,再拼接括号里的path,你path随便写不存在的文件依然能输出,说明根本没校验你的文件路径,需要你自己填写文件的相对路径
so what the question is ?