Python Forum
getting a full path string from a pathlib.PurePath object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting a full path string from a pathlib.PurePath object
#1
is the proper way to get the plain string path of a pathlib.PurePath object or pathlib.Path object to pass it to str() and use what that returns? that is all i can find. the documentation (i have the 3.5.2 PDF) only describes the .name attribute for part of the path. i suppose i could join the .parts value in some way.

    import pathlib
    ...
    fp = pathlib.Path('foo/bar')
    ...
    fn = str(fp)
this is only the 2nd time using this module and the 1st time didn't need this.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
In [1]: import pathlib

In [2]: path = pathlib.Path('.')

In [3]: full_path = path.absolute()

In [4]: my_path = full_path.as_posix()

In [5]: print(my_path)
/home/victor

In [6]: type(my_path)
Out[6]: str
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
or use path.resolve()
Reply
#4
(Mar-20-2018, 11:08 AM)Larz60+ Wrote: or use path.resolve()
that appears to return a Path object instead of a string, although it might be good to apply this first.

edit:

maybe ... if the object does not exist, .resolve() raises a fit.

just to be sure it's understood, the code has a pathlib.Path() or pathlib.PurePath() object and needs a string to work with and .name is not the full path.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Since pathlib returns an object, you can also do things like:
homepage = Path('.')
doc = homepage / '..' / 'doc'
# Create the directory if it doesn't already exist
doc.mkdir(exist_ok=True)
datapath = homepage / 'data'
datapath.mkdir(exist_ok=True)

input_file = datapath / 'MyFile.txt'

with input_file.open() as f:
    for line in f:
        print(line)
Reply
#6
Hah, didn't know that it has open() method! Smile
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
What I don't like is the method relative_to.
This method works only, if the given argument is a subpath:

pathlib.Path.home().relative_to('/dev')
Error:
ValueError: '/home/andre' does not start with '/dev'
Doing the same with the os.path
home = os.path.expanduser('~')
print(os.path.relpath(home, '/dev'))
Output:
../home/andre
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
but i needed a real string and the only thing i could find was attribute method .__str__() which is what str() uses.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
(Mar-21-2018, 08:12 AM)Skaperen Wrote: but i needed a real string and the only thing i could find was attribute method .__str__() which is what str() uses.

Have you noticed this: https://python-forum.io/Thread-getting-a...4#pid42634 ?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
str(your_path.resolve())

The method resolve returns a new Path object.
Path objects have a magic method for str and bytes (__str__, __bytes__).

str(pathobject) -> returns a str of the path
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  -i option changes sys.path (removes leading empty string '') markanth 6 1,975 Aug-26-2022, 09:27 PM
Last Post: markanth
  Pathlib import not working chriswrcg 9 3,665 May-29-2022, 07:37 PM
Last Post: snippsat
  Convert string to path using Python 2.7 tester_V 10 6,400 Nov-20-2021, 02:20 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,198 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  deleting an empty Dir, using pathlib.Path tester_V 9 5,778 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Trying to pathlib instead of os.path tester_V 4 2,472 Jun-22-2021, 04:15 AM
Last Post: tester_V
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,190 May-02-2021, 03:45 PM
Last Post: Anldra12
  pathlib destpath.exists() true even file does not exist NaN 9 4,653 Dec-01-2020, 12:43 PM
Last Post: NaN
  How to convert a string "<... object at POINTER>" to an object? mandaxyz 5 3,634 Aug-08-2020, 10:44 PM
Last Post: mandaxyz
  C-API for Python 3 - Get String from Object mga010 2 3,594 Jun-23-2020, 04:32 PM
Last Post: mga010

Forum Jump:

User Panel Messages

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