Python Forum
pathlib: resolving a path that does not exist
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pathlib: resolving a path that does not exist
#1
i am using pathlib.Path and need to resolve a path before creating a directory there. it may not exist, yet. even its parent may not exist. even its grandparent may not exist. and so on. but, i will be doing .mkdir(parents=True,exist_ok=True) on it to create a directory there. the need to resolve is to produce a proper message before the mkdir() is attempted. how can i do the equivent of .resolve() for a non-existing object?

i was going to get its parent and do the resolve() on that. but even the parent may not exist.
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
You can do something like:
>>> from pathlib import Path
>>> home = Path('.')
>>> if home.parent.exists():
...     print('has parent')
... 
has parent
>>>
just give current path a name (home in this instance).
it would probably be wise to also assure the current directory at the beginning with
os.chdir(os.path.dirname(__file__))
Reply
#3
i don't specifically need to know whether the path exists or not, although if that fact is part of a solution, well, ok. what i need is a canonical path of the directory before it is possibly created (along with any necessary parents). if the directory does already exist, it will just be used. but the message that will have the canonical path (from .resolve()) in it needs to be produce before the possible creation. then the application will be putting things in it (the hardlinks issue). i'd like to do all this with pathlib although making the hardlinks will be done with os.link().

i also want to resolve things like "woot/foo/../bar" even though "woot/foo" will never exist. but if "woot/foo" already exists and is not a directory, failing at that point is desired (but not essential). and this being relative to the current directory means its path will be included. user names may be involved ("~skaperen/foo/../../python/./lib/.") as well as absolute paths.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
I haven't tried it, but i don't think there's anything to stop you from defining a path before it exists,
so long as you don't try to do things with the destination.
Reply
#5
but the .resolve() method fails if the path does not exist. i need what it does for a path whether it exists or not. so i am looking for an implementation of something it that works even if the path does not exist. shortcut methods that resolve the parent cannot be used because the parent might not exist.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
You could go back up the ancestor paths until you find one that exists. If there is none, the path cannot be resolved.
Reply
#7
there is a certain degree of resolving that be done with paths that totally do not exist. for example if you have "/foo/../" you can assume "foo" is a directory and substitute "/foo/../" with "/" and the like ("/bar/foo/.././../" becomes "/"). even if you have some upper directory that exists and these cases below it, you have to decide if will make that assumption. and, of course, non-existing levels say nothing about possibly being a symlink, so this can always be guessed wrong.
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
  UndefinedEnvironmentName: 'extra' does not exist in evaluation environment EarthAndMoon 3 1,678 Oct-09-2023, 05:38 PM
Last Post: snippsat
  Resolving ImportError: No module named gdb (Python in C++) mandaxyz 3 1,412 Oct-04-2023, 02:43 PM
Last Post: mandaxyz
  Pathlib import not working chriswrcg 9 3,661 May-29-2022, 07:37 PM
Last Post: snippsat
  check if a file exist on the internet and get the size kucingkembar 6 1,757 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  SQLALCHEMY - Column doesn't exist jamesaarr 9 7,526 Nov-04-2021, 09:20 AM
Last Post: ndc85430
  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
  mySQL Database error not resolving. cybertooth 2 3,193 Aug-30-2021, 05:45 PM
Last Post: ibreeden
  deleting an empty Dir, using pathlib.Path tester_V 9 5,777 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
  pathlib destpath.exists() true even file does not exist NaN 9 4,653 Dec-01-2020, 12:43 PM
Last Post: NaN

Forum Jump:

User Panel Messages

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