0

This question is essentially two parts: how does Pathlib handle an unknown number of path segments as arguments and is it possible to pass a list or tuple into the constructor?

The documentation for the Path class is as follows:

class pathlib.Path(*pathsegments)

I know it will accept a variable number of arguments, so I figured it may also check the type and then expand a list or tuple as appropriate. Testing has shown it doesn't do that, however:

import pathlib

parts = ['a', 'b', 'c', 'd']
p = pathlib.Path(parts)

which results in:

TypeError: expected str, bytes or os.PathLike object, not list

The same thing happens with a tuple. How can I use Pathlib to construct a path out of an unknown number of path segments?

Anthony
  • 1,760
  • 1
  • 23
  • 43

1 Answers1

1

Call it like this:

p = pathlib.Path(*parts)
omajid
  • 14,165
  • 4
  • 47
  • 64