user3000139
user3000139

Reputation: 11

python : join() called with 2 arguments in list comprehension?

I am new to python. This code is from "Dive Into Python" by Mark Pilgrim, it appears that join() is being called with 2 args, it works fine:

dirname="/usr/"
[f for f in os.listdir(dirname) if os.path.isdir(os.path.join(dirname,f))]

But if you try:

smthn="data"
smthnelse="otherdata"
print "\n".join(smthn,smthnelse)

We get an error that join() can take only one argument.

Upvotes: 0

Views: 3404

Answers (2)

saudi_Dev
saudi_Dev

Reputation: 839

Look Using str.join() like this:

smthn="data"
smthnelse="otherdata"

print( "\n",smthn.join(smthnelse) )

Because you are put smthnelse between the spaces in smthn >>"data"

os.path.join() not look like str.join

the second for string and seq and the first only for File System paths.

Upvotes: 0

Daniel
Daniel

Reputation: 42748

os.path.join takes as arguments an arbitrary number of strings, str.join instead takes as one argument a iterable providing strings. These two functions are individual functions.

Upvotes: 4

Related Questions