美文网首页
python绝对路径和相对路径

python绝对路径和相对路径

作者: gz_tester | 来源:发表于2018-04-25 19:12 被阅读0次

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

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

相关文章

网友评论

      本文标题:python绝对路径和相对路径

      本文链接:https://www.haomeiwen.com/subject/tfgylftx.html