pyhton之os.path

目录结构

file

__file__表示了当前文件的path

以相对路径运行:python 1.py
结果:1.py
以绝对路径运行:python F:\python-study\test\1.py
结果:F:\python-study\test\1.py
os.path.dirname(path)

去掉文件名,返回目录,若print os.path.dirname(file)所在脚本是以绝对路径运行的,则会输出该脚本所在的绝对路径,若以相对路径运行,输出空目录

os.path.abspath(file)

获取当前文件的绝对路径,无论脚本是以绝对路径运行还是以相对路径运行

猜你喜欢

转载自www.cnblogs.com/raind/p/9500997.html
今日推荐