python绝对路径和相对路径

原创文章,转载请注明出处

1、我们随便找个文件,获取它的系统路径,比如:

debugtalk.py路径:/Users/wechat-applet-script/wechatInterfaceTestCases/testCase/debugtalk.py

a、相对路径

path = os.path.dirname("debugtalk.py")

b、绝对路径

path = os.path.abspath("debugtalk.py")

c、获取当前文件下的父目录-相对路径

path = os.path.dirname(os.path.dirname(__file__))
#  打印结果:/Users/wechat-applet-script/wechatInterfaceTestCases

d、获取当前文件所在的目录-相对路径

# 方法一:
path = os.path.dirname(os.path.abspath("debugtalk.py"))
# 方法二:
path = os.path.dirname(__file__)
#  打印结果:/Users/wechat-applet-script/wechatInterfaceTestCases/testCase
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容