How to resolve relative paths in python?

后端 未结 4 1015
遇见更好的自我
遇见更好的自我 2021-02-18 12:57

I have Directory structure like this

projectfolder/fold1/fold2/fold3/script.py

now I\'m giving script.py a path as commandline argument of a fi

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-18 13:16

    import os
    dir = os.path.dirname(__file__)
    path = raw_input()
    if os.path.isabs(path):
        print "input path is absolute"
    else:
        path = os.path.join(dir, path)
        print "absolute path is %s" % path
    

    Use os.path.isabs to judge if input path is absolute or relative, if it is relative, then use os.path.join to convert it to absolute

提交回复
热议问题