os.path.basename适用于URL,为什么? [英] os.path.basename works with URLs, why?

查看:67
本文介绍了os.path.basename适用于URL,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

..并且我认为os.path.*仅适用于本地路径,而不适用于URL?请注意,上面的示例也是在Windows上运行的,结果相似.

.. 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的许多功能仅仅是字符串操作功能(对于路径操作而言, happen 特别方便)- -并且由于它是无害的,并且有时会派上用场,尽管通常说不正确",但我怀疑这种情况很快就会改变-有关更多详细信息,请在shell/命令提示符下使用以下简单的单行代码:

In practice many functions of os.path are just string manipulation functions (which just happen to 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:

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

或者,对于Python 3:

Or, for 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:])"

这篇关于os.path.basename适用于URL,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆