1

i want to pass string (dirPath + dirName[x]) to a function

i already tried 3 times to combining string and a specific list (only dirName[x])

it seems that python just rejecting it anyway even if i already convert it to another type.

is anyone know how i pass this string to Function?

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)

error2:

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

try3:

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

error3:

dir = '-'.join([dirPath,dirName[x]])
TypeError: sequence item 0: expected str instance, list found
2
  • Can you please clarify what the objective of your code, or that particular line is? Commented Sep 11, 2017 at 15:01
  • my object is to passing string .. dirPath (a string) and dirName[x] (a string within a list) to a function Commented Sep 11, 2017 at 15:08

1 Answer 1

1

Looks like dirPath is a list. You need to convert it to a string (for example using str.join() if you just want all the elements concatenated) before you concatenate the next part:

''.join(dirPath) + dirName[x]
Sign up to request clarification or add additional context in comments.

1 Comment

i just realised that dirPath is a list. there is a typo in my code. i should assign string to dirPath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.