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
35 def _get_bothseps(path): argument
36 if isinstance(path, bytes):
43 # (this is done by normpath).
81 # Return whether a path is absolute.
83 # For Windows it is absolute if it starts with a slash or backslash (current
84 # volume), or if a pathname after the volume-letter-and-colon or UNC-resource
88 """Test whether a path is absolute"""
99 # Absolute: UNC, device, and paths with a drive and root.
100 # LEGACY BUG: isabs("/x") should be false since the path has no drive.
107 def join(path, *paths): argument
108 path = os.fspath(path)
109 if isinstance(path, bytes):
119 path[:0] + sep #23780: Ensure compatible data type even if p is null.
120 result_drive, result_path = splitdrive(path)
124 # Second path is absolute
131 # Different drives => ignore the first path entirely
137 # Second path is relative to the first
138 if result_path and result_path[-1] not in seps:
141 ## add separator between UNC and non-absolute path
143 result_drive and result_drive[-1:] != colon):
147 genericpath._check_arg_types('join', path, *paths)
151 # Split a path in a drive specification (a drive letter followed by a
152 # colon) and the path specification.
153 # It is always true that drivespec + pathspec == p
155 """Split a pathname into drive/UNC sharepoint and relative path specifiers.
156 Returns a 2-tuple (drive_or_unc, path); either part may be empty.
160 It is always true that:
163 If the path contained a drive letter, drive_or_unc will contain everything
166 If the path contained a UNC path, the drive_or_unc will contain the host name
170 Paths cannot contain both a drive letter and a UNC path.
191 if index == -1:
194 if index2 == -1:
198 # Drive-letter drives, e.g. X:
203 # Split a path in head (everything up to the last '/') and tail (the
204 # rest). After the trailing '/' is stripped, the invariant
206 # The resulting head won't end in '/' unless it is the root.
211 Return tuple (head, tail) where tail is everything after the final slash.
218 while i and p[i-1] not in seps:
219 i -= 1
226 # Split a path in root and extension.
227 # The extension is everything starting at the last dot in the last
228 # pathname component; the root is everything before that.
229 # It is always true that root + ext == p.
240 # Return the tail (basename) part of a path.
247 # Return the head (dirname) part of a path.
253 # Is a path a symbolic link?
256 def islink(path): argument
257 """Test whether a path is a symbolic link.
261 st = os.lstat(path)
266 # Being true for dangling symbolic links is also useful.
268 def lexists(path): argument
269 """Test whether a path exists. Returns True for broken symbolic links"""
271 st = os.lstat(path)
276 # Is a path a mount point?
285 # fails if the drive letter is the result of a SUBST.
290 def ismount(path): argument
291 """Test whether a path is a mount point (a drive root, the root of a
293 path = os.fspath(path)
294 seps = _get_bothseps(path)
295 path = abspath(path)
296 root, rest = splitdrive(path)
303 x = path.rstrip(seps)
304 y =_getvolumepathname(path).rstrip(seps)
312 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
313 # the path is returned unchanged (leaving error reporting to whatever
314 # function is called with the expanded path as argument).
316 # (A function should also be defined to do full *sh-style environment
319 def expanduser(path): argument
322 If user or $HOME is unknown, do nothing."""
323 path = os.fspath(path)
324 if isinstance(path, bytes):
328 if not path.startswith(tilde):
329 return path
330 i, n = 1, len(path)
331 while i < n and path[i] not in _get_bothseps(path):
337 return path
346 target_user = path[1:i]
355 # normal profile directory, this guess is likely wrong,
358 return path
361 if isinstance(path, bytes):
364 return userhome + path[i:]
369 # - no expansion within single quotes
370 # - '$$' is translated into '$'
371 # - '%%' is translated into '%' if '%%' are not seen in %var1%%var2%
372 # - ${varname} is accepted.
373 # - $varname is accepted.
374 # - %varname% is accepted.
375 # - varnames can be made out of letters, digits and the characters '_-'
376 # (though is not verified in the ${varname} and %varname% cases)
380 def expandvars(path): argument
384 path = os.fspath(path)
385 if isinstance(path, bytes):
386 if b'$' not in path and b'%' not in path:
387 return path
389 varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii')
397 if '$' not in path and '%' not in path:
398 return path
400 varchars = string.ascii_letters + string.digits + '_-'
407 res = path[:0]
409 pathlen = len(path)
411 c = path[index:index+1]
413 path = path[index + 1:]
414 pathlen = len(path)
416 index = path.index(c)
417 res += c + path[:index + 1]
419 res += c + path
420 index = pathlen - 1
422 if path[index + 1:index + 2] == percent:
426 path = path[index+1:]
427 pathlen = len(path)
429 index = path.index(percent)
431 res += percent + path
432 index = pathlen - 1
434 var = path[:index]
436 if environ is None:
444 if path[index + 1:index + 2] == dollar:
447 elif path[index + 1:index + 2] == brace:
448 path = path[index+2:]
449 pathlen = len(path)
451 index = path.index(rbrace)
453 res += dollar + brace + path
454 index = pathlen - 1
456 var = path[:index]
458 if environ is None:
466 var = path[:0]
468 c = path[index:index + 1]
472 c = path[index:index + 1]
474 if environ is None:
482 index -= 1
489 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
491 # but as this module is called "ntpath", that's obviously wrong!
496 def normpath(path): argument
497 """Normalize path, eliminating double slashes, etc."""
498 path = os.fspath(path)
499 if isinstance(path, bytes):
509 path = path.replace(altsep, sep)
510 prefix, path = splitdrive(path)
513 if path.startswith(sep):
515 path = path.lstrip(sep)
517 comps = path.split(sep)
523 if i > 0 and comps[i-1] != pardir:
524 del comps[i-1:i+1]
525 i -= 1
532 # If the path is now empty, substitute '.'
538 def normpath(path): argument
539 """Normalize path, eliminating double slashes, etc."""
540 path = os.fspath(path)
541 if isinstance(path, bytes):
542 return os.fsencode(_path_normpath(os.fsdecode(path))) or b"."
543 return _path_normpath(path) or "."
546 def _abspath_fallback(path): argument
547 """Return the absolute version of a path as a fallback function in case
548 `nt._getfullpathname` is not available or raises OSError. See bpo-31047 for
553 path = os.fspath(path)
554 if not isabs(path):
555 if isinstance(path, bytes):
559 path = join(cwd, path)
560 return normpath(path)
562 # Return an absolute path.
566 except ImportError: # not running on Windows - mock up something sensible
570 def abspath(path): argument
571 """Return the absolute version of a path."""
573 return _getfullpathname(normpath(path))
575 return _abspath_fallback(path)
580 # realpath is a no-op on systems without _getfinalpathname support.
583 def _readlink_deep(path): argument
585 # return the path we currently have.
601 while normcase(path) not in seen:
602 seen.add(normcase(path))
604 old_path = path
605 path = _nt_readlink(path)
608 if not isabs(path):
611 # just return the old path.
613 path = old_path
615 path = normpath(join(dirname(old_path), path))
623 return path
625 def _getfinalpathname_nonstrict(path): argument
626 # These error codes indicate that we should stop resolving the path
645 # Non-strict algorithm is to find as much of the target directory
647 tail = path[:0]
648 while path:
650 path = _getfinalpathname(path)
651 return join(path, tail) if tail else path
656 # The OS could not resolve this path fully, so we attempt
659 new_path = _readlink_deep(path)
660 if new_path != path:
665 path, name = split(path)
666 # TODO (bpo-38186): Request the real file name from the directory
667 # entry using FindFirstFileW. For now, we will return the path
669 if path and not name:
670 return path + tail
674 def realpath(path, *, strict=False): argument
675 path = normpath(path)
676 if isinstance(path, bytes):
681 # bpo-38081: Special case for realpath(b'nul')
682 if normcase(path) == normcase(os.fsencode(devnull)):
689 # bpo-38081: Special case for realpath('nul')
690 if normcase(path) == normcase(devnull):
692 had_prefix = path.startswith(prefix)
693 if not had_prefix and not isabs(path):
694 path = join(cwd, path)
696 path = _getfinalpathname(path)
699 # gh-106242: Raised for embedded null characters
701 # Non-strict mode returns the path as-is, since we've already
702 # made it absolute.
705 path = normpath(path)
710 path = _getfinalpathname_nonstrict(path)
711 # The path returned by _getfinalpathname will always start with \\?\ -
713 # path.
714 if not had_prefix and path.startswith(prefix):
717 if path.startswith(unc_prefix):
718 spath = new_unc_prefix + path[len(unc_prefix):]
720 spath = path[len(prefix):]
721 # Ensure that the non-prefixed path resolves to the same path
723 if _getfinalpathname(spath) == path:
724 path = spath
726 # Unexpected, as an invalid path should not have gained a prefix
730 # If the path does not exist and originally did not exist, then
733 path = spath
734 return path
741 def relpath(path, start=None): argument
742 """Return a relative version of a path"""
743 path = os.fspath(path)
744 if isinstance(path, bytes):
753 if start is None:
756 if not path:
757 raise ValueError("no path specified")
762 path_abs = abspath(normpath(path))
766 raise ValueError("path is on mount %r, start on mount %r" % (
771 # Work out how much of the filepath is shared by start and path.
778 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
783 genericpath._check_arg_types('relpath', path, start)
787 # Return the longest common sub-path of the sequence of paths given as input.
788 # The function is case-insensitive and 'separator-insensitive', i.e. if the
789 # only difference between two paths is the use of '\' versus '/' as separator,
792 # However, the returned path will have the standard '\' separator (even if the
794 # first path given in the sequence. Additionally, any trailing separator is
795 # stripped from the returned path.
798 """Given a sequence of path names, returns the longest common sub-path."""
801 raise ValueError('commonpath() arg is an empty sequence')
820 raise ValueError("Can't mix absolute and relative paths") from None
822 # Check that all drive letters or UNC paths match. The check is made only
828 drive, path = splitdrive(paths[0].replace(altsep, sep))
829 common = path.split(sep)
851 # attribute to tell whether or not the path is a directory.
852 # This is overkill on Windows - just pass the path to GetFileAttributes