Python - 传递字符串和特定列表,组合成函数

时间:2017-09-11 14:53:09

标签: string python-3.x list function recursion

我想将字符串(dirPath + dirName [x])传递给函数

我已经尝试过三次组合字符串和特定列表(只有dirName [x])

似乎python只是拒绝它,即使我已经将它转换为另一种类型。

有谁知道我如何将此字符串传递给函数?

layer = 0
dirName = []
#dummy value layer = 4,  dirName = [/a,/b,/c,/d], dirBruteForce(layer,vault)

def dirBruteForce(layer,dirPath):
if layer > 0 :
    #call again to make layered dir
    for x in range(len(dirName)):
        dirBruteForce(layer - 1, dirPath + dirName[x]) ##this line
#call end of the dir
elif layer is 0 :
    for x in range(len(dirName)):
        try:
            os.makedirs(dirPath)
            print(dirPath)
        except Exception as e:
            pass
###

try1:

dirBruteForce(layer - 1, dirPath + dirName[x])
TypeError: can only concatenate list (not "str") to list

try2:

list = [dirPath,str(dirName[x])]
dir = ''.join(list)
dirBruteForce(layer - 1, dir)

误差2:

dir = ''.join(list)
TypeError: sequence item 0: expected str instance, list found

try3:

dir = ''.join([dirPath,dirName[x]])
dirBruteForce(layer - 1, dir)

误差3:

dir = '-'.join([dirPath,dirName[x]])
TypeError: sequence item 0: expected str instance, list found

1 个答案:

答案 0 :(得分:1)

看起来dirPath是一个列表。在连接下一部分之前,您需要将其转换为字符串(例如,如果您只想连接所有元素,请使用str.join()):

''.join(dirPath) + dirName[x]