这是 bash:
❯ basename /x/y/
y
❯ basename /x/y
y
这是 R:
> fs::path("/x/y") %>% fs::path_file()
[1] "y"
> fs::path("/x/y/") %>% fs::path_file()
[1] "y"
这是 python:
>>> os.path.basename('/x/y')
'y'
>>> os.path.basename('/x/y/')
''
并没有全面调查,不过我猜可能大多数脚本语言都和 bash 的行为相同。请问有谁知道 python 这么设计的原因吗?
1
jackyzy823 2023-05-04 16:09:06 +08:00 2
|
2
ovovovovo 2023-05-04 16:48:04 +08:00
os.path.basename(path)¶
Return the base name of pathname path. This is the second element of the pair returned by passing path to the function split(). Note that the result of this function is different from the Unix basename program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an empty string (''). 原因不知道,我只知道 doc 是这么介绍的 |
3
sunamask OP @jackyzy823 作者也吃过亏…… "tripped me up in various ways",看来除了死记硬背之外没别的办法了……
|