简体   繁体   English

列表中的 Python os.path.join()

[英]Python os.path.join() on a list

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?我在这里缺少什么?

The problem is, os.path.join doesn't take a list as argument, it has to be separate arguments.问题是, os.path.join不将list作为参数,它必须是单独的参数。

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'

Assuming join wasn'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 3 Python 3 的提示

reduce was moved to the functools module. reduce已移至functools模块。

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.注意第一个参数,它不会改变结果。

It's just the method.这只是方法。 You're not missing anything.你没有错过任何东西。 The official documentation shows that you can use list unpacking to supply several paths:官方文档显示可以使用list unpacking来提供几个路径:

s = "c:/,home,foo,bar,some.txt".split(",")
os.path.join(*s)

Note the *s intead of just s in os.path.join(*s) .*s这一翻译的只是sos.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.使用星号将触发列表的解包,这意味着每个列表参数将作为单独的参数提供给函数。

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]

reduce is builtin in Python 2.x. reduce内置于 Python 2.x 中。 In Python 3.x it has been moved to itertools However the accepted the answer is better.在 Python 3.x 中,它已移至itertools但是公认的答案更好。

This has been answered below but answering if you have a list of items that needs to be joined.这已在下面回答,但如果您有需要加入的项目列表,请回答。

if you do it in python 3, you will get an error: 如果您在python 3中执行此操作,则会收到错误消息:

>>> s = "c:/,home,foo,bar,some.txt".split(",")
>>> s
['c:/', 'home', 'foo', 'bar', 'some.txt']
>>> os.path.join(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/wangjunyu/.pyenv/versions/3.7.0/lib/python3.7/posixpath.py", line 80, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not list

in python 2, just nothing happened. 在python 2中,什么也没发生。 os.path.join(s) result is the same with print(s) . os.path.join(s)结果与print(s)相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM