英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论