本文整理汇总了Python中ntpath.isabs方法的典型用法代码示例。如果您正苦于以下问题:Python ntpath.isabs方法的具体用法?Python ntpath.isabs怎么用?Python ntpath.isabs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ntpath
的用法示例。
在下文中一共展示了ntpath.isabs方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: canonical_filename
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def canonical_filename(filename):
"""Return a canonical file name for `filename`.
An absolute path with no redundant components and normalized case.
"""
if filename not in CANONICAL_FILENAME_CACHE:
if not os.path.isabs(filename):
for path in [os.curdir] + sys.path:
if path is None:
continue
f = os.path.join(path, filename)
try:
exists = os.path.exists(f)
except UnicodeError:
exists = False
if exists:
filename = f
break
cf = abs_file(filename)
CANONICAL_FILENAME_CACHE[filename] = cf
return CANONICAL_FILENAME_CACHE[filename]
示例2: canonical_filename
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def canonical_filename(filename):
"""Return a canonical file name for `filename`.
An absolute path with no redundant components and normalized case.
"""
if filename not in CANONICAL_FILENAME_CACHE:
cf = filename
if not os.path.isabs(filename):
for path in [os.curdir] + sys.path:
if path is None:
continue
f = os.path.join(path, filename)
try:
exists = os.path.exists(f)
except UnicodeError:
exists = False
if exists:
cf = f
break
cf = abs_file(cf)
CANONICAL_FILENAME_CACHE[filename] = cf
return CANONICAL_FILENAME_CACHE[filename]
示例3: __init__
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def __init__(self, path, root=Root.builddir, destdir=False):
if destdir and isinstance(root, Root):
raise ValueError('destdir only applies to install paths')
drive, path = self.__normalize(path, expand_user=True)
if posixpath.isabs(path):
root = Root.absolute
destdir = False
elif root == Root.absolute:
raise ValueError("'{}' is not absolute".format(path))
elif isinstance(root, BasePath):
path = self.__join(root.suffix, path)
destdir = root.destdir
root = root.root
if ( path == posixpath.pardir or
path.startswith(posixpath.pardir + posixpath.sep) ):
raise ValueError("too many '..': path cannot escape root")
self.suffix = drive + path
self.root = root
self.destdir = destdir
示例4: test_isabs
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def test_isabs(self):
tester('ntpath.isabs("c:\\")', 1)
tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
tester('ntpath.isabs("\\foo")', 1)
tester('ntpath.isabs("\\foo\\bar")', 1)
示例5: validate_abspath
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def validate_abspath(self, value: PathType) -> None:
value = str(value)
is_posix_abs = posixpath.isabs(value)
is_nt_abs = ntpath.isabs(value)
err_object = ValidationError(
description=(
"an invalid absolute file path ({}) for the platform ({}).".format(
value, self.platform.value
)
+ " to avoid the error, specify an appropriate platform correspond"
+ " with the path format, or 'auto'."
),
platform=self.platform,
reason=ErrorReason.MALFORMED_ABS_PATH,
)
if any([self._is_windows() and is_nt_abs, self._is_linux() and is_posix_abs]):
return
if self._is_universal() and any([is_posix_abs, is_nt_abs]):
ValidationError(
description=(
"{}. expected a platform independent file path".format(
"POSIX absolute file path found"
if is_posix_abs
else "NT absolute file path found"
)
),
platform=self.platform,
reason=ErrorReason.MALFORMED_ABS_PATH,
)
if any([self._is_windows(), self._is_universal()]) and is_posix_abs:
raise err_object
drive, _tail = ntpath.splitdrive(value)
if not self._is_windows() and drive and is_nt_abs:
raise err_object
示例6: validate_abspath
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def validate_abspath(self, value: str) -> None:
if any([ntpath.isabs(value), posixpath.isabs(value)]):
raise ValidationError(
description="found an absolute path ({}), expected a filename".format(value),
platform=self.platform,
reason=ErrorReason.FOUND_ABS_PATH,
)
示例7: isabs_anywhere
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def isabs_anywhere(filename):
"""Is `filename` an absolute path on any OS?"""
return ntpath.isabs(filename) or posixpath.isabs(filename)
示例8: __normalize
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def __normalize(path, expand_user=False):
if expand_user:
path = os.path.expanduser(path)
drive, path = ntpath.splitdrive(path)
if drive and not ntpath.isabs(path):
raise ValueError('relative paths with drives not supported')
drive = drive.replace('\\', '/')
path = posixpath.normpath(path.replace('\\', '/'))
if path == posixpath.curdir:
path = ''
return drive, path
示例9: append
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def append(self, path):
drive, path = self.__normalize(path, expand_user=True)
if not posixpath.isabs(path):
path = self.__join(self.suffix, path)
return type(self)(drive + path, self.root, self.destdir)
示例10: test_path_isabs
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def test_path_isabs(self):
self.assertPathEqual(self.path.isabs)
示例11: _validate_resource_path
# 需要导入模块: import ntpath [as 别名]
# 或者: from ntpath import isabs [as 别名]
def _validate_resource_path(path):
"""
Validate the resource paths according to the docs.
https://setuptools.readthedocs.io/en/latest/pkg_resources.html#basic-resource-access
>>> warned = getfixture('recwarn')
>>> warnings.simplefilter('always')
>>> vrp = NullProvider._validate_resource_path
>>> vrp('foo/bar.txt')
>>> bool(warned)
False
>>> vrp('../foo/bar.txt')
>>> bool(warned)
True
>>> warned.clear()
>>> vrp('/foo/bar.txt')
>>> bool(warned)
True
>>> vrp('foo/../../bar.txt')
>>> bool(warned)
True
>>> warned.clear()
>>> vrp('foo/f../bar.txt')
>>> bool(warned)
False
Windows path separators are straight-up disallowed.
>>> vrp(r'\\foo/bar.txt')
Traceback (most recent call last):
...
ValueError: Use of .. or absolute path in a resource path \
is not allowed.
>>> vrp(r'C:\\foo/bar.txt')
Traceback (most recent call last):
...
ValueError: Use of .. or absolute path in a resource path \
is not allowed.
"""
invalid = (
os.path.pardir in path.split(posixpath.sep) or
posixpath.isabs(path) or
ntpath.isabs(path)
)
if not invalid:
return
msg = "Use of .. or absolute path in a resource path is not allowed."
# Aggressively disallow Windows absolute paths
if ntpath.isabs(path) and not posixpath.isabs(path):
raise ValueError(msg)
# for compatibility, warn; in future
# raise ValueError(msg)
warnings.warn(
msg[:-1] + " and will raise exceptions in a future release.",
DeprecationWarning,
stacklevel=4,
)