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
#11
(Mar-21-2018, 09:50 AM)DeaD_EyE Wrote: 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

see my original post.

(Mar-21-2018, 09:50 AM)DeaD_EyE Wrote: 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

also: resolve requires that the file exist. we need a PurePath version of that just for resolving /../. also a Path version for resovling both /../ and symlinks that gracefully ends when a non-existing directory is encountered.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#12
The method resolve works also, when the Path doesn't exist:
I made a test. This example Path doesn't exist on my system.
In [16]: pathlib.Path('../dev/foo/bar/baz/../proc/foo.txt').resolve()
Out[16]: PosixPath('/home/dev/foo/bar/proc/foo.txt')
Maybe you are using an older Python version, where it's not fixed. I use always 3.6.

Use resolve if you want to follow symlinks and use absolute, if you want not to follow symlinks.
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
#13
i am using 3.5.2. that's what comes in Ubuntu 16.04.3 LTS. maybe 18.04 will fix this and include 3.6. but i want my code that uses pathlib to work on 16.04.X systems.

as for my original question, i found documentation that says str() is the right way to get just a string. it was in the "Operators" section, which i skipped over when i found a / thing that acted what i thought was wrong.

i don't think of str() as an operator but, apparently, BDFL does, so i should do that, at least in a Python context.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
Python 3.6.3 | Ubuntu 17.10

In [4]: pathlib.Path('../dev/foo/baz/../baz/andre').resolve()
Out[4]: PosixPath('/home/dev/foo/baz/andre')
Python 3.5.3 | Debian 9.3
pathlib.Path('../dev/foo/baz/../baz/andre').resolve()
Error:
FileNotFoundError: [Errno 2] No such file or directory: '/dev/foo'
The answer why this happens, is here: https://docs.python.org/3/library/pathli...th.resolve
Quote:If the path doesn’t exist and strict is True, FileNotFoundError is raised. If strict is False, the path is resolved as far as possible and any remainder is appended without checking whether it exists. If an infinite loop is encountered along the resolution path, RuntimeError is raised.

New in version 3.6: The strict argument.

If you're able to install build tools on your system, you can use pyenv.
One another option could be, that you try to install this: https://pypi.python.org/pypi/pathlib2/
Or you make your own helpers for the method resolve.
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
#15
i need to find documentation on how pyenv works in a technical sense, before i ever run it on my system. else i will have to isolate it in a VM or some other instance.

i have not yet decided if i will upgrade to Ubuntu 18.04 LTS, yet. i might just do it in a VM giving it the whole 2nd 1TB drive my laptop has. then it will be easier to run it on real hardware.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
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,130 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