81

The following code:

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Desktop + "/subdir"

gets the following error:

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
    <ipython-input-4-eb31bbeb869b> in <module>()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

I'm clearly doing something shady here, but it raises the question: How do I access a subdirectory of a Path object?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63

2 Answers2

133
  • The correct operator to extend a pathlib object is /
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
  • When several absolute paths are given, the last is taken as an anchor (mimicking os.path.join()’s behavior):
>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
  • In a Windows path, changing the local root doesn’t discard the previous drive setting:
>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
  • Refer to the documentation for addition details pertaining to giving an absolute path, such as Path('/subdir').

Resources:

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
  • These code examples (at least the first) error. One of the issues is `WindowsPath` isn't imported. But after it is, I get this error (py 3.9.10) when I create instances: `File "/usr/lib/python3.9/pathlib.py", line 1084, in __new__\n\traise NotImplementedError("cannot instantiate %r on your system"\nNotImplementedError: cannot instantiate 'WindowsPath' on your system` I'm using cygwin so it might be related. – Daniel Kaplan Mar 23 '23 at 23:27
36

What you're looking for is:

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")

the joinpath() function will append the second parameter to the first and add the '/' for you.

BEWARE SubDeskTop = Path.joinpath(Desktop, "/subdir") won't work. The slash before subdir ruins it. (From Patrik in the comments).

Areza
  • 5,623
  • 7
  • 48
  • 79
r.ook
  • 13,466
  • 2
  • 22
  • 39
  • 10
    That works, though I learned the '/' operator is overloaded to do the same thing. – Ray Salemi Jan 10 '18 at 15:48
  • Good point. I mostly use the `path` module in `os` so the usage is a bit different. `Path` creates a path object but in `os.path` the returned object is a string so it couldn't call that function on itself. Good to know, TIL. – r.ook Jan 10 '18 at 16:27
  • 9
    BEWARE: `SubDeskTop = Path.joinpath(Desktop, "/subdir")` won't work. The slash before `subdir` ruins it. – PatrickT Jun 15 '20 at 10:41