Python| os.path.isabs() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。 os.path模块是Python中OS 模块的子模块,用于常见的路径名操作。
Python中的os.path.isabs()
方法用于检查指定路径是否为绝对路径。
在 Unix 平台上,绝对路径以正斜杠 ('/') 开头,而在 Windows 上,绝对路径在删除任何潜在驱动器号后以反斜杠 ('\') 开头。
Syntax: os.path.isabs(path)
Parameter:
path: A path-like object representing a file system path.
A path-like object is either a string or bytes object representing a path.
Return Type: This method returns a Boolean value of class bool. This method returns True if specified path is an absolute path, otherwise returns False.
代码 #1:使用os.path.isabs()
方法(在 Unix 平台上)
# Python program to explain os.path.isabs() method
# importing os.path module
import os.path
# Path
path = '/home/User/Documents'
# Check whether the
# specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
# Path
path = 'home/User/Documents/'
# Check whether the
# specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
# Path
path = '../User/Documents/'
# Check whether the
# specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
True
False
False
代码 #2:使用os.path.isabs()
方法(在 Windows 上)
# Python program to explain os.path.isabs() method
# importing os.path module
import os.path
# Path
path = r"C:\\Users\\Documents"
# Check whether the
# specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
# Path
path = r"\\User\\Documents"
# Check whether the
# specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
# Path
path = r"User\\Documents"
# Check whether the
# specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
True
True
False
参考: https://docs。 Python.org/3/library/os.path.html