Getting Home Directory with pathlib
Question:
Looking through the new pathlib
module in Python 3.4, I notice that there isn’t any simple way to get the user’s home directory. The only way I can come up with for getting a user’s home directory is to use the older os.path
lib like so:
import pathlib
from os import path
p = pathlib.Path(path.expanduser("~"))
This seems clunky. Is there a better way?
Answers:
It seems that this method was brought up in a bug report here. Some code was written (given here) but unfortunately it doesn’t seem that it made it into the final Python 3.4 release.
Incidentally the code that was proposed was extremely similar to the code you have in your question:
# As a method of a Path object
def expanduser(self):
""" Return a new path with expanded ~ and ~user constructs
(as returned by os.path.expanduser)
"""
return self.__class__(os.path.expanduser(str(self)))
EDIT
Here is a rudimentary subclassed version PathTest
which subclasses WindowsPath
(I’m on a Windows box but you could replace it with PosixPath
). It adds a classmethod
based on the code that was submitted in the bug report.
from pathlib import WindowsPath
import os.path
class PathTest(WindowsPath):
def __new__(cls, *args, **kwargs):
return super(PathTest, cls).__new__(cls, *args, **kwargs)
@classmethod
def expanduser(cls):
""" Return a new path with expanded ~ and ~user constructs
(as returned by os.path.expanduser)
"""
return cls(os.path.expanduser('~'))
p = PathTest('C:/')
print(p) # 'C:/'
q = PathTest.expanduser()
print(q) # C:UsersUsername
Caveat: This answer is 3.4 specific. As pointed out in other answers, this functionality was added in later versions.
It looks like there is no better way to do it. I just searched the documentation and nothing relevant came up for my search terms.
~
has zero hits
expand
has zero hits
user
has 1 hit as a return value for Path.owner()
relative
has 8 hits, mostly related to PurePath.relative_to()
As of python-3.5, there is pathlib.Path.home()
, which improves the situation somewhat.
The result on Windows is
>>>pathlib.Path.home()
WindowsPath('C:/Users/username')
and on Linux
>>>pathlib.Path.home()
PosixPath('/home/username')
There is method expanduser()
:
p = PosixPath('~/films/Monty Python')
p.expanduser()
PosixPath('/home/eric/films/Monty Python')
For people who are lazy to read the comments :
Now there is the pathlib.Path.home
method.
Looking through the new pathlib
module in Python 3.4, I notice that there isn’t any simple way to get the user’s home directory. The only way I can come up with for getting a user’s home directory is to use the older os.path
lib like so:
import pathlib
from os import path
p = pathlib.Path(path.expanduser("~"))
This seems clunky. Is there a better way?
It seems that this method was brought up in a bug report here. Some code was written (given here) but unfortunately it doesn’t seem that it made it into the final Python 3.4 release.
Incidentally the code that was proposed was extremely similar to the code you have in your question:
# As a method of a Path object
def expanduser(self):
""" Return a new path with expanded ~ and ~user constructs
(as returned by os.path.expanduser)
"""
return self.__class__(os.path.expanduser(str(self)))
EDIT
Here is a rudimentary subclassed version PathTest
which subclasses WindowsPath
(I’m on a Windows box but you could replace it with PosixPath
). It adds a classmethod
based on the code that was submitted in the bug report.
from pathlib import WindowsPath
import os.path
class PathTest(WindowsPath):
def __new__(cls, *args, **kwargs):
return super(PathTest, cls).__new__(cls, *args, **kwargs)
@classmethod
def expanduser(cls):
""" Return a new path with expanded ~ and ~user constructs
(as returned by os.path.expanduser)
"""
return cls(os.path.expanduser('~'))
p = PathTest('C:/')
print(p) # 'C:/'
q = PathTest.expanduser()
print(q) # C:UsersUsername
Caveat: This answer is 3.4 specific. As pointed out in other answers, this functionality was added in later versions.
It looks like there is no better way to do it. I just searched the documentation and nothing relevant came up for my search terms.
~
has zero hitsexpand
has zero hitsuser
has 1 hit as a return value forPath.owner()
relative
has 8 hits, mostly related toPurePath.relative_to()
As of python-3.5, there is pathlib.Path.home()
, which improves the situation somewhat.
The result on Windows is
>>>pathlib.Path.home()
WindowsPath('C:/Users/username')
and on Linux
>>>pathlib.Path.home()
PosixPath('/home/username')
There is method expanduser()
:
p = PosixPath('~/films/Monty Python')
p.expanduser()
PosixPath('/home/eric/films/Monty Python')
For people who are lazy to read the comments :
Now there is the pathlib.Path.home
method.