python os.path.basename 适用于 URL,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1112545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:30:46  来源:igfitidea点击:

os.path.basename works with URLs, why?

pythonurlpath

提问by Sridhar Ratnakumar

>>> os.path.basename('http://example.com/file.txt')
'file.txt'

.. and I thought os.path.*work only on local paths and not URLs? Note that the above example was run on Windows too .. with similar result.

.. 我认为os.path.*只适用于本地路径而不适用于 URL?请注意,上面的示例也在 Windows 上运行.. 具有类似的结果。

回答by Alex Martelli

In practice many functions of os.pathare just string manipulation functions (which just happento be especially handy for path manipulation) -- and since that's innocuous and occasionally handy, while formally speaking "incorrect", I doubt this will change anytime soon -- for more details, use the following simple one-liner at a shell/command prompt:

在实践中的许多功能os.path都只是字符串处理函数(这只是碰巧成为路径操作尤其方便) -因为做法无伤大雅,偶尔派上用场,而正式地说“不正确”,我怀疑很快这将改变-更多详细信息,请在 shell/命令提示符下使用以下简单的单行:

$ python -c"import sys; import StringIO; x = StringIO.StringIO(); sys.stdout = x; import this; sys.stdout = sys.__stdout__; print x.getvalue().splitlines()[10][9:]"

Or, for Python 3:

或者,对于 Python 3:

$ python -c"import sys; import io; x = io.StringIO(); sys.stdout = x; import this; sys.stdout = sys.__stdout__; print(x.getvalue().splitlines()[10][9:])"

回答by sunqiang

On windows, look at the source code: C:\Python25\Lib\ntpath.py

在 windows 上,查看源代码:C:\Python25\Lib\ntpath.py

def basename(p):
    """Returns the final component of a pathname"""
    return split(p)[1]

os.path.split (in the same file) just split "\" (and sth. else)

os.path.split (在同一个文件中)只是拆分“\”(和其他)

回答by Wojciech Bederski

Use the source Luke:

使用来源卢克:


def basename(p):
    """Returns the final component of a pathname"""
    i = p.rfind('/') + 1
    return p[i:]

Edit (response to clarification):

编辑(对澄清的回应):

It works for URLs by accident, that's it. Because of that, exploiting its behaviour could be considered code smell by some.

它偶然适用于 URL,仅此而已。因此,利用其行为可能被某些人认为是代码异味。

Trying to "fix" it (check if passed path is not url) is also surprisingly difficult

尝试“修复”它(检查传递的路径是否不是 url)也非常困难

www.google.com/test.php
[email protected]/12
./src/bin/doc/goto.c

are at the same time correct pathnames and URLs (relative), so is the http:/hello.txt(one /, and only on linux, and it's kinda stupid :)). You could "fix" it for absolute urls but relative ones will still work. Handling one special case in differently is a big no no in the python world.

同时是正确的路径名和 URL(相对),也是http:/hello.txt(一个 /,并且仅在 linux 上,这有点愚蠢:))。您可以为绝对网址“修复”它,但相对网址仍然有效。在 python 世界中,以不同的方式处理一个特殊情况是一个很大的禁忌。

To sum it up: import this

总结一下:导入这个

回答by Martin M.

Beware of URLs with parameters, anchors or anything that isn't a "plain" URL:

谨防带有参数、锚点或任何非“纯”URL 的 URL:

>>> import os.path
>>> os.path.basename("protocol://fully.qualifie.host/path/to/file.txt")
'file.txt'
>>> os.path.basename("protocol://fully.qualifie.host/path/to/file.txt?param1&param1#anchor")
'file.txt?param1&param1#anchor'

回答by JCF

Forward slash is also an acceptable path delimiter in Windows.

正斜杠也是 Windows 中可接受的路径分隔符。

It is merely that the command line does not accept paths that begin with a / because that character is reserved for args switches.

只是命令行不接受以 / 开头的路径,因为该字符是为 args 开关保留的。

回答by Ted Percival

Why? Because it's useful for parsing URLs as well as local file paths. Why not?

为什么?因为它对于解析 URL 以及本地文件路径很有用。为什么不?