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
My code examples are always for Python >=3.6.0
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
My code examples are always for Python >=3.6.0
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
  deleting an empty Dir, using pathlib.Path tester_V 9 374 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Trying to pathlib instead of os.path tester_V 4 264 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 834 May-02-2021, 03:45 PM
Last Post: Anldra12
  How to convert a string "<... object at POINTER>" to an object? mandaxyz 5 1,091 Aug-08-2020, 10:44 PM
Last Post: mandaxyz
  C-API for Python 3 - Get String from Object mga010 2 990 Jun-23-2020, 04:32 PM
Last Post: mga010
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 8,129 May-08-2020, 04:26 AM
Last Post: bowlofred
  String to File Path creedX 4 1,041 Apr-06-2020, 07:29 PM
Last Post: creedX
  Question about if with () or without () / pathlib Tecuma 3 746 Apr-02-2020, 10:02 AM
Last Post: Tecuma
  converting string object inside a list into an intiger bwdu 4 861 Mar-31-2020, 10:36 AM
Last Post: buran
  How to get full path of specified hidden files matching pattern recursively SriRajesh 4 1,132 Jan-18-2020, 07:12 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

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