如何区分相对路径和绝对路径

2007向凤盏 2007向凤盏 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

绝对路径:以根目录开始,一直到该文件所在为绝对路径,绝对路径日常中使用相对较少,一般绝对路径用在正式的场合。

相对路径:相对于当前目录的位置。

 代码分析如下:

path = os.getcwd()
path1 = os.path.join(path,"test.txt")

def dir_check():
    print("the pwd is:%s" % path)
    print("the full path is:%s" % path1)
    with open(path1) as file:
        css = file.readlines()
        print("%s" % css)

def print_dir():
    print("sys.path[0] = ", sys.path[0])
    print("__file__ = ", __file__)
    print("os.path.abspath(__file__) = ", os.path.abspath(__file__))
    print("os.path.realpath(__file__) = ", os.path.realpath(__file__))
if __name__ == '__main__':
    dir_check()
    print_dir()

 

...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
2007向凤盏
42python 124 成员 589 内容