Lines Matching +full:path +full:- +full:is +full:- +full:absolute
1 # Module 'ntpath' -- common operations on WinNT/Win95 pathnames
5 module as os.path.
8 # strings representing various path-related bits and pieces
36 def _get_bothseps(path): argument
37 if isinstance(path, bytes):
44 # (this is done by normpath).
82 # Return whether a path is absolute.
84 # For Windows it is absolute if it starts with a slash or backslash (current
85 # volume), or if a pathname after the volume-letter-and-colon or UNC-resource
89 """Test whether a path is absolute"""
100 # Absolute: UNC, device, and paths with a drive and root.
101 # LEGACY BUG: isabs("/x") should be false since the path has no drive.
108 def join(path, *paths): argument
109 path = os.fspath(path)
110 if isinstance(path, bytes):
120 path[:0] + sep #23780: Ensure compatible data type even if p is null.
121 result_drive, result_path = splitdrive(path)
125 # Second path is absolute
132 # Different drives => ignore the first path entirely
138 # Second path is relative to the first
139 if result_path and result_path[-1] not in seps:
142 ## add separator between UNC and non-absolute path
144 result_drive and result_drive[-1:] != colon):
148 genericpath._check_arg_types('join', path, *paths)
152 # Split a path in a drive specification (a drive letter followed by a
153 # colon) and the path specification.
154 # It is always true that drivespec + pathspec == p
156 """Split a pathname into drive/UNC sharepoint and relative path specifiers.
157 Returns a 2-tuple (drive_or_unc, path); either part may be empty.
161 It is always true that:
164 If the path contained a drive letter, drive_or_unc will contain everything
167 If the path contained a UNC path, the drive_or_unc will contain the host name
171 Paths cannot contain both a drive letter and a UNC path.
192 if index == -1:
195 if index2 == -1:
199 # Drive-letter drives, e.g. X:
204 # Split a path in head (everything up to the last '/') and tail (the
205 # rest). After the trailing '/' is stripped, the invariant
207 # The resulting head won't end in '/' unless it is the root.
212 Return tuple (head, tail) where tail is everything after the final slash.
219 while i and p[i-1] not in seps:
220 i -= 1
227 # Split a path in root and extension.
228 # The extension is everything starting at the last dot in the last
229 # pathname component; the root is everything before that.
230 # It is always true that root + ext == p.
241 # Return the tail (basename) part of a path.
248 # Return the head (dirname) part of a path.
254 # Is a path a symbolic link?
257 def islink(path): argument
258 """Test whether a path is a symbolic link.
262 st = os.lstat(path)
267 # Being true for dangling symbolic links is also useful.
269 def lexists(path): argument
270 """Test whether a path exists. Returns True for broken symbolic links"""
272 st = os.lstat(path)
277 # Is a path a mount point?
286 # fails if the drive letter is the result of a SUBST.
291 def ismount(path): argument
292 """Test whether a path is a mount point (a drive root, the root of a
294 path = os.fspath(path)
295 seps = _get_bothseps(path)
296 path = abspath(path)
297 root, rest = splitdrive(path)
304 x = path.rstrip(seps)
305 y =_getvolumepathname(path).rstrip(seps)
313 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
314 # the path is returned unchanged (leaving error reporting to whatever
315 # function is called with the expanded path as argument).
317 # (A function should also be defined to do full *sh-style environment
320 def expanduser(path): argument
323 If user or $HOME is unknown, do nothing."""
324 path = os.fspath(path)
325 if isinstance(path, bytes):
329 if not path.startswith(tilde):
330 return path
331 i, n = 1, len(path)
332 while i < n and path[i] not in _get_bothseps(path):
338 return path
347 target_user = path[1:i]
356 # normal profile directory, this guess is likely wrong,
359 return path
362 if isinstance(path, bytes):
365 return userhome + path[i:]
370 # - no expansion within single quotes
371 # - '$$' is translated into '$'
372 # - '%%' is translated into '%' if '%%' are not seen in %var1%%var2%
373 # - ${varname} is accepted.
374 # - $varname is accepted.
375 # - %varname% is accepted.
376 # - varnames can be made out of letters, digits and the characters '_-'
377 # (though is not verified in the ${varname} and %varname% cases)
381 def expandvars(path): argument
385 path = os.fspath(path)
386 if isinstance(path, bytes):
387 if b'$' not in path and b'%' not in path:
388 return path
390 varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii')
398 if '$' not in path and '%' not in path:
399 return path
401 varchars = string.ascii_letters + string.digits + '_-'
408 res = path[:0]
410 pathlen = len(path)
412 c = path[index:index+1]
414 path = path[index + 1:]
415 pathlen = len(path)
417 index = path.index(c)
418 res += c + path[:index + 1]
420 res += c + path
421 index = pathlen - 1
423 if path[index + 1:index + 2] == percent:
427 path = path[index+1:]
428 pathlen = len(path)
430 index = path.index(percent)
432 res += percent + path
433 index = pathlen - 1
435 var = path[:index]
437 if environ is None:
445 if path[index + 1:index + 2] == dollar:
448 elif path[index + 1:index + 2] == brace:
449 path = path[index+2:]
450 pathlen = len(path)
452 index = path.index(rbrace)
454 res += dollar + brace + path
455 index = pathlen - 1
457 var = path[:index]
459 if environ is None:
467 var = path[:0]
469 c = path[index:index + 1]
473 c = path[index:index + 1]
475 if environ is None:
483 index -= 1
490 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
492 # but as this module is called "ntpath", that's obviously wrong!
497 def normpath(path): argument
498 """Normalize path, eliminating double slashes, etc."""
499 path = os.fspath(path)
500 if isinstance(path, bytes):
510 path = path.replace(altsep, sep)
511 prefix, path = splitdrive(path)
514 if path.startswith(sep):
516 path = path.lstrip(sep)
518 comps = path.split(sep)
524 if i > 0 and comps[i-1] != pardir:
525 del comps[i-1:i+1]
526 i -= 1
533 # If the path is now empty, substitute '.'
539 def normpath(path): argument
540 """Normalize path, eliminating double slashes, etc."""
541 path = os.fspath(path)
542 if isinstance(path, bytes):
543 return os.fsencode(_path_normpath(os.fsdecode(path))) or b"."
544 return _path_normpath(path) or "."
547 def _abspath_fallback(path): argument
548 """Return the absolute version of a path as a fallback function in case
549 `nt._getfullpathname` is not available or raises OSError. See bpo-31047 for
554 path = os.fspath(path)
555 if not isabs(path):
556 if isinstance(path, bytes):
560 path = join(cwd, path)
561 return normpath(path)
563 # Return an absolute path.
567 except ImportError: # not running on Windows - mock up something sensible
571 def abspath(path): argument
572 """Return the absolute version of a path."""
574 return _getfullpathname(normpath(path))
576 return _abspath_fallback(path)
581 # realpath is a no-op on systems without _getfinalpathname support.
582 def realpath(path, *, strict=False): argument
583 return abspath(path)
585 def _readlink_deep(path, ignored_error=OSError): argument
587 # return the path we currently have.
603 while normcase(path) not in seen:
604 seen.add(normcase(path))
606 old_path = path
607 path = _nt_readlink(path)
610 if not isabs(path):
613 # just return the old path.
615 path = old_path
617 path = normpath(join(dirname(old_path), path))
625 return path
627 def _getfinalpathname_nonstrict(path, ignored_error=OSError): argument
628 # These error codes indicate that we should stop resolving the path
647 # Non-strict algorithm is to find as much of the target directory
649 tail = path[:0]
650 while path:
652 path = _getfinalpathname(path)
653 return join(path, tail) if tail else path
658 # The OS could not resolve this path fully, so we attempt
661 new_path = _readlink_deep(path,
663 if new_path != path:
668 path, name = split(path)
669 # TODO (bpo-38186): Request the real file name from the directory
670 # entry using FindFirstFileW. For now, we will return the path
672 if path and not name:
673 return path + tail
677 def realpath(path, *, strict=False): argument
678 path = normpath(path)
679 if isinstance(path, bytes):
684 # bpo-38081: Special case for realpath(b'nul')
685 if normcase(path) == normcase(os.fsencode(devnull)):
692 # bpo-38081: Special case for realpath('nul')
693 if normcase(path) == normcase(devnull):
695 had_prefix = path.startswith(prefix)
697 if strict is ALLOW_MISSING:
705 if not had_prefix and not isabs(path):
706 path = join(cwd, path)
708 path = _getfinalpathname(path)
711 # gh-106242: Raised for embedded null characters
713 # Non-strict mode returns the path as-is, since we've already
714 # made it absolute.
717 path = normpath(path)
720 path = _getfinalpathname_nonstrict(path,
722 # The path returned by _getfinalpathname will always start with \\?\ -
724 # path.
725 if not had_prefix and path.startswith(prefix):
728 if path.startswith(unc_prefix):
729 spath = new_unc_prefix + path[len(unc_prefix):]
731 spath = path[len(prefix):]
732 # Ensure that the non-prefixed path resolves to the same path
734 if _getfinalpathname(spath) == path:
735 path = spath
737 # Unexpected, as an invalid path should not have gained a prefix
741 # If the path does not exist and originally did not exist, then
744 path = spath
745 return path
752 def relpath(path, start=None): argument
753 """Return a relative version of a path"""
754 path = os.fspath(path)
755 if isinstance(path, bytes):
764 if start is None:
767 if not path:
768 raise ValueError("no path specified")
773 path_abs = abspath(normpath(path))
777 raise ValueError("path is on mount %r, start on mount %r" % (
782 # Work out how much of the filepath is shared by start and path.
789 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
794 genericpath._check_arg_types('relpath', path, start)
798 # Return the longest common sub-path of the sequence of paths given as input.
799 # The function is case-insensitive and 'separator-insensitive', i.e. if the
800 # only difference between two paths is the use of '\' versus '/' as separator,
803 # However, the returned path will have the standard '\' separator (even if the
805 # first path given in the sequence. Additionally, any trailing separator is
806 # stripped from the returned path.
809 """Given a sequence of path names, returns the longest common sub-path."""
812 raise ValueError('commonpath() arg is an empty sequence')
831 raise ValueError("Can't mix absolute and relative paths") from None
833 # Check that all drive letters or UNC paths match. The check is made only
839 drive, path = splitdrive(paths[0].replace(altsep, sep))
840 common = path.split(sep)
862 # attribute to tell whether or not the path is a directory.
863 # This is overkill on Windows - just pass the path to GetFileAttributes