Python Forum

Full Version: 'import Path' do not understand how to use it
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings!
You guys pointed me to this module - 'import Path' and I'm trying to understand how to use it.
Seached Google, found now simple examples, all the same jibberish about 'home" directory and 'Path.cwd()'
I'm sure there are a lot of people looking for a simple explanation on how to use it.
For example:
from pathlib import Path
home = Path.home()
wave_absolute = Path(home, "ocean", "wave.txt")
print(f" Home Dir ------------- {home}" )
print(f" Absolute path\\file -- {wave_absolute}")
What if a directory is not 'home' but 'c:\\02'
I tried to make changes to the snipped but it errors out.
src = 'c:\\02'
home = Path.src()
Or
home = Path('c:\\02')
I'm sure the module is a great tool but documents are horrible and examples are awful...
Thank you in advance!
Path is a python module. So anything after a dot like Path.home() must be either an attribute or a method, and that should be in the docs. Path is a bit odd because of all the different classes running around and sharing methods. But the documentation for it is here: Path.home

This method tries to find the home directory of the user running python and returns it as a Path object. If you just want a random directory as a path object, pass it to Path(). That's exactly what you show as your second example, but you didn't include the error, so we don't know what is happening.

from pathlib import Path

home = Path('c:\\02')
print(home)
Output:
c:\02
Thank you!