Lines Matching refs:path
5 module as os.path.
8 # strings representing various path-related bits and pieces
35 def _get_bothseps(path):
36 if isinstance(path, bytes):
81 # Return whether a path is absolute.
88 """Test whether a path is absolute"""
100 # LEGACY BUG: isabs("/x") should be false since the path has no drive.
107 def join(path, *paths):
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
141 ## add separator between UNC and non-absolute path
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.
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.
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.
203 # Split a path in head (everything up to the last '/') and tail (the
226 # Split a path in root and extension.
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):
257 """Test whether a path is a symbolic link.
261 st = os.lstat(path)
268 def lexists(path):
269 """Test whether a path exists. Returns True for broken symbolic links"""
271 st = os.lstat(path)
276 # Is a path a mount point?
290 def ismount(path):
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).
319 def expanduser(path):
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]
358 return path
361 if isinstance(path, bytes):
364 return userhome + path[i:]
380 def expandvars(path):
384 path = os.fspath(path)
385 if isinstance(path, bytes):
386 if b'$' not in path and b'%' not in path:
387 return path
397 if '$' not in path and '%' not in path:
398 return path
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
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
434 var = path[:index]
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
456 var = path[:index]
466 var = path[:0]
468 c = path[index:index + 1]
472 c = path[index:index + 1]
489 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
496 def normpath(path):
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)
532 # If the path is now empty, substitute '.'
538 def normpath(path):
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):
547 """Return the absolute version of a path as a fallback function in case
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.
570 def abspath(path):
571 """Return the absolute version of a path."""
573 return _getfullpathname(normpath(path))
575 return _abspath_fallback(path)
583 def _readlink_deep(path):
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):
626 # These error codes indicate that we should stop resolving the path
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)
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):
675 path = normpath(path)
676 if isinstance(path, bytes):
682 if normcase(path) == normcase(os.fsencode(devnull)):
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)
701 # Non-strict mode returns the path as-is, since we've already
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):
742 """Return a relative version of a path"""
743 path = os.fspath(path)
744 if isinstance(path, bytes):
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.
783 genericpath._check_arg_types('relpath', path, start)
787 # Return the longest common sub-path of the sequence of paths given as input.
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."""
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