Question
Asked By – ATOzTOA
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?
Now we will see solution for issue: Python os.path.join() on a list
Answer
The problem is, os.path.join
doesn’t take a list
as argument, it has to be separate arguments.
To unpack the list into separate arguments required by join
(and for the record: list was obtained from a string using split
), use *
– or the ‘splat’ operator, thus:
>>> s = "c:/,home,foo,bar,some.txt".split(",")
>>> os.path.join(*s)
'c:/home\\foo\\bar\\some.txt'
This question is answered By – ATOzTOA
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0