列表中的 Python os.path.join()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14826888/
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
Python os.path.join() on a list
提问by ATOzTOA
I can do
我可以
>>> os.path.join("c:/","home","foo","bar","some.txt")
'c:/home\foo\bar\some.txt'
But, when I do
但是,当我这样做
>>> s = "c:/,home,foo,bar,some.txt".split(",")
>>> os.path.join(s)
['c:/', 'home', 'foo', 'bar', 'some.txt']
What am I missing here?
我在这里缺少什么?
采纳答案by ATOzTOA
The problem is, os.path.joindoesn't take a listas argument, it has to be separate arguments.
问题是,os.path.join不接受 alist作为参数,它必须是单独的参数。
This is where *, the 'splat' operator comes into play...
这就是*“splat”运算符发挥作用的地方......
I can do
我可以
>>> s = "c:/,home,foo,bar,some.txt".split(",")
>>> os.path.join(*s)
'c:/home\foo\bar\some.txt'
回答by Greg
It's just the method. You're not missing anything. The official documentationshows that you can use list unpacking to supply several paths:
这只是方法。你没有错过任何东西。在官方文件显示,你可以用列表拆包提供几条路径:
s = "c:/,home,foo,bar,some.txt".split(",")
os.path.join(*s)
Note the *sintead of just sin os.path.join(*s). Using the asterisk will trigger the unpacking of the list, which means that each list argument will be supplied to the function as a separate argument.
请注意,*s只是这一翻译s在os.path.join(*s)。使用星号将触发列表的解包,这意味着每个列表参数将作为单独的参数提供给函数。
回答by Thorsten Kranz
Assuming joinwasn't designed that way (which it is, as ATOzTOA pointed out), and it only took two parameters, you could still use the built-in reduce:
假设join不是那样设计的(正如 ATOzTOA 指出的那样),并且它只需要两个参数,您仍然可以使用内置的reduce:
>>> reduce(os.path.join,["c:/","home","foo","bar","some.txt"])
'c:/home\foo\bar\some.txt'
Same output like:
相同的输出,如:
>>> os.path.join(*["c:/","home","foo","bar","some.txt"])
'c:/home\foo\bar\some.txt'
Just for completeness and educational reasons (and for other situations where *doesn't work).
只是出于完整性和教育原因(以及其他*不起作用的情况)。
Hint for Python 3For Python 3, reducewas moved to the functoolsmodule.
Python 3 的提示对于 Python 3,reduce已移至functools模块。
回答by Sebastian Mach
I stumbled over the situation where the list might be empty. In that case:
我偶然发现了列表可能为空的情况。在这种情况下:
os.path.join('', *the_list_with_path_components)
Note the first argument, which will not alter the result.
注意第一个参数,它不会改变结果。
回答by Nishant
This can be also thought of as a simple map reduce operation if you would like to think of it from a functional programming perspective.
如果您想从函数式编程的角度考虑,这也可以被认为是一个简单的 map reduce 操作。
import os
folders = [("home",".vim"),("home","zathura")]
[reduce(lambda x,y: os.path.join(x,y), each, "") for each in folders]
reduceis builtin in Python 2.x. In Python 3.x it has been moved to itertoolsHowever the accepted the answer is better.
reduce内置于 Python 2.x 中。在 Python 3.x 中,它已移至itertools但是接受的答案更好。
This has been answered below but answering if you have a list of items that needs to be joined.
这已在下面回答,但如果您有需要加入的项目列表,请回答。

