Python Forum

Full Version: pathlib: resolving a path that does not exist
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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__))
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.
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.
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.
You could go back up the ancestor paths until you find one that exists. If there is none, the path cannot be resolved.
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.