So the only case where this would make a difference is when calling a
"function acting on the symbolic link itself" (such as lstat() or
unlink()) on a path with a trailing slash:
>>> os.lstat('foo')
os.stat_result(st_mode=41471, st_ino=1981954, st_dev=2050, st_nlink=1,
st_uid=1000, st_gid=1000, st_size=4, st_atime=1407370025,
st_mtime=1407370025, st_ctime=1407370025)
>>> os.lstat('foo/')
os.stat_result(st_mode=17407, st_ino=917505, st_dev=2050, st_nlink=7,
st_uid=0, st_gid=0, st_size=4096, st_atime=1407367916,
st_mtime=1407369857, st_ctime=1407369857)
>>> pathlib.Path('foo').lstat()
os.stat_result(st_mode=41471, st_ino=1981954, st_dev=2050, st_nlink=1,
st_uid=1000, st_gid=1000, st_size=4, st_atime=1407370037,
st_mtime=1407370025, st_ctime=1407370025)
>>> pathlib.Path('foo/').lstat()
os.stat_result(st_mode=41471, st_ino=1981954, st_dev=2050, st_nlink=1,
st_uid=1000, st_gid=1000, st_size=4, st_atime=1407370037,
st_mtime=1407370025, st_ctime=1407370025)
But you can also call resolve() explicitly if you want to act on the
link target rather than the link itself:
>>> pathlib.Path('foo/').resolve().lstat()
os.stat_result(st_mode=17407, st_ino=917505, st_dev=2050, st_nlink=7,
st_uid=0, st_gid=0, st_size=4096, st_atime=1407367916,
st_mtime=1407369857, st_ctime=1407369857)
Am I overlooking other cases?
Regards
Antoine.
Am I overlooking other cases?
pathlib is generally concerned with filesystem operations written in
Python, not arbitrary third-party tools. Also it is probably easy to
append the trailing slash in your command-line invocation, if so desired.
> Le 06/08/2014 20:50, Alexander Belopolsky a écrit :
> > There are many interfaces where trailing slash is significant. […]
> > Loosing it when passing path strings through pathlib.Path() may be a
> > source of bugs.
>
> pathlib is generally concerned with filesystem operations written in
> Python, not arbitrary third-party tools.
The operating system shell is more than an “arbitrary third-party tool”,
though; it preserves paths, and handles invoking commands.
You seem to be saying that ‘pathlib’ is not intended to be helpful for
constructing a shell command. Will its documentation warn that is so?
> Also it is probably easy to append the trailing slash in your
> command-line invocation, if so desired.
The trouble is that one can desire it, and construct a path knowing that
the presence or absence of a trailing slash has semantic significance;
and then have it unaccountably altered by the pathlib.Path code. This is
worse than preserving the semantic value.
--
\ “But Marge, what if we chose the wrong religion? Each week we |
`\ just make God madder and madder.” —Homer, _The Simpsons_ |
_o__) |
Ben Finney
pathlib lets you do operations on paths. It also gives you a string
representation of the path that's expected to designate that path when
talking to operating system APIs. It doesn't give you the possibility to
store other semantic variations ("whether a new directory level must be
created"); that's up to you to add those.
(similarly, it doesn't have separate classes to represent "a file", "a
directory", "a non-existing file", etc.)
Regards
Antoine.