How to resolve relative paths in python?

后端 未结 4 989
遇见更好的自我
遇见更好的自我 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

    A practical example:

    sys.argv[0] gives you the name of the current script

    os.path.dirname() gives you the relative directory name

    thus, the next line, gives you the absolute working directory of the current executing file.

    cwd = os.path.abspath(os.path.dirname(sys.argv[0]))

    Personally, I always use this instead of os.getcwd() since it gives me the script absolute path, independent of the directory from where the script was called.

提交回复
热议问题