os.path.basename适用于URL,为什么? [英] os.path.basename works with URLs, why?
问题描述
>>> 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
的许多功能仅仅是字符串操作功能(对于路径操作而言,
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屋!