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.
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.
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
  -i option changes sys.path (removes leading empty string '') markanth 6 1,901 Aug-26-2022, 09:27 PM
Last Post: markanth
  Pathlib import not working chriswrcg 9 3,524 May-29-2022, 07:37 PM
Last Post: snippsat
  Convert string to path using Python 2.7 tester_V 10 6,278 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,151 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  deleting an empty Dir, using pathlib.Path tester_V 9 5,675 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Trying to pathlib instead of os.path tester_V 4 2,432 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,112 May-02-2021, 03:45 PM
Last Post: Anldra12
  pathlib destpath.exists() true even file does not exist NaN 9 4,567 Dec-01-2020, 12:43 PM
Last Post: NaN
  How to convert a string "<... object at POINTER>" to an object? mandaxyz 5 3,540 Aug-08-2020, 10:44 PM
Last Post: mandaxyz
  C-API for Python 3 - Get String from Object mga010 2 3,549 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