Python Forum
getting a full path string from a pathlib.PurePath object - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: getting a full path string from a pathlib.PurePath object (/thread-9077.html)

Pages: 1 2


RE: getting a full path string from a pathlib.PurePath object - Skaperen - Mar-22-2018

(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.


RE: getting a full path string from a pathlib.PurePath object - DeaD_EyE - Mar-22-2018

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.


RE: getting a full path string from a pathlib.PurePath object - Skaperen - Mar-23-2018

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.


RE: getting a full path string from a pathlib.PurePath object - DeaD_EyE - Mar-23-2018

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/pathlib.html#pathlib.Path.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.


RE: getting a full path string from a pathlib.PurePath object - Skaperen - Mar-24-2018

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.