Difference between pathlib.Path().resolve() and pathlib.Path().parent

huangapple go评论96阅读模式
英文:

Difference between pathlib.Path().resolve() and pathlib.Path().parent

问题

I'm having a bit of a hard time understanding the difference between pathlib.Path().resolve() and pathlib.Path().parent.

如果我想要获取当前脚本的目录,这两者是否具有相同的功能?

And are both of these location-agnostic, i.e. if I were to move the files to a different location, would my script still run?

这两者是否与位置无关,即如果我将文件移动到不同的位置,我的脚本是否仍然能够运行?

英文:

I'm having a bit of a hard time understanding the difference between pathlib.Path().resolve() and pathlib.Path().parent.

If I want to be able to get the directory of the current script, do these serve the same purpose?

And are both of these location-agnostic, i.e. if I were to move the files to a different location, would my script still run?

答案1

得分: 1

The method pathlib.Path().resolve() will return an absolute path. If the path is already absolute the method will have no effect.

For example assuming you are in the working directory /home/python and run the following:

In: pathlib.Path().resolve()
Out: "/home/python"

In: pathlib.Path(".").resolve()
Out: "/home/python"

In: pathlib.Path("foo/bar.txt").resolve()
Out: "/home/python/foo/bar.txt"

In: pathlib.Path("/home/python").resolve()
Out: "/home/python"

To explain the example above, if you instantiate a path class pathlib.Path() without any arguments it will use . as default value, which refers to the current working directory. So the first two examples both resolve to /home/python.

foo/bar.txt is a relative path. If your working directory is /home/python it will resolve to the absolute path /home/python/foo/bar.txt. If you are in the working directory /root it will resolve to /root/foo/bar.txt.


pathlib.Path().parent is another path object representing the parent of the path given.

# In working directory /root
In: pathlib.Path("/home/python").parent
Out: PosixPath("/home")

In: pathlib.Path("/home/python").parent.resolve()
Out: "/home"

# In working directory /home/python
In: pathlib.Path("foo").parent
Out: PosixPath(".")

In: pathlib.Path("foo").parent.resolve()
Out: "/home/python"

Yes your script will still run in other directories if you watch out for certain things.

# /home/python/foo.py

print(pathlib.Path().resolve())

Running this script from the directory /home/python will result in a path like this /home/python.

If you run the script from /home like this python/foo.py it will result in a path like this /home.

If you watch out for potholes like this you should be fine to run your script from any directory. Doing mistakes here usually results in file not found errors.

英文:

The method pathlib.Path().resolve() will return an absolute path. If the path is already absolute the method will have no effect.

For example assuming you are in the working directory /home/python and run the following

In: pathlib.Path().resolve()
Out: "/home/python"

In: pathlib.Path(".").resolve()
Out: "/home/python"

In: pathlib.Path("foo/bar.txt").resolve()
Out: "/home/python/foo/bar.txt"

In: pathlib.Path("/home/python").resolve()
Out: "/home/python"

To explain the example above, if you instantiate a path class pathlib.Path() without any arguments it will use . as default value, which refers to the current working directory. So the first two examples both resolve to /home/python.

foo/bar.txt is a relative path. If your working directory is /home/python it will resolve to the absolute path /home/python/foo/bar.txt. If you are in the working directory /root it will resolve to /root/foo/bar.txt.


pathlib.Path().parent is another path object representing the parent of the path given.

# In working directory /root
In: pathlib.Path("/home/python").parent
Out: PosixPath("/home")

In: pathlib.Path("/home/python").parent.resolve()
Out: "/home"

# In working directory /home/python
In: pathlib.Path("foo").parent
Out: PosixPath(".")

In: pathlib.Path("foo").parent.resolve()
Out: "/home/python"

Yes your script will still run in other directories if you watch out for certain things.

# /home/python/foo.py

print(pathlib.Path().resolve())

Running this script from the directory /home/python will result in a path like this /home/python.

If you run the script from /home like this python/foo.py it will result in a path like this /home

If you watch out for potholes like this you should be fine to run your script from any directory. Doing mistakes here usually results in file not found errors.

huangapple
  • 本文由 发表于 2023年6月12日 00:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定