-6ren">
gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:33:47 25 4
gpt4 key购买 nike

我可以的

>>> os.path.join("c:/","home","foo","bar","some.txt")
'c:/home\\foo\\bar\\some.txt'

但是,当我这样做时

>>> s = "c:/,home,foo,bar,some.txt".split(",")
>>> os.path.join(s)
['c:/', 'home', 'foo', 'bar', 'some.txt']

我在这里错过了什么?

最佳答案

问题是,os.path.join 不以 list 作为参数,它必须是单独的参数。

要将列表解包 放入 join 所需的单独参数中(并且为了记录:列表是使用 split 从字符串中获得的), 使用 * - 或 'splat' 运算符,因此:

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

关于列表中的 Python os.path.join(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14826888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com