Python Forum
'import Path' do not understand how to use it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'import Path' do not understand how to use it
#1
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!
Reply
#2
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
and tester_V like this post
Reply
#3
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  beginner doesn't understand import cimerio 3 500 Feb-12-2025, 05:02 PM
Last Post: cimerio
  import a function from another file using relative path paul18fr 6 2,971 Aug-01-2024, 06:40 AM
Last Post: paul18fr
  Import a module one step back of the path prathampatel9 1 1,796 Sep-21-2022, 01:34 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 3,171 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  import path SpongeB0B 5 3,767 Apr-01-2020, 06:29 AM
Last Post: SpongeB0B
  ImportError: cannot import name 'path' user123 2 13,794 Jul-14-2019, 10:08 AM
Last Post: Larz60+
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 7,544 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908
  Trying to understand how import works in python patrick99e99 3 4,844 Jun-12-2018, 04:48 AM
Last Post: patrick99e99

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020