Python abspath如何确定路径是否是相对的?

Python abspath如何确定路径是否是相对的?,python,operating-system,Python,Operating System,我知道abspath可以获取一个文件或一组相对的文件,并通过预先设置当前目录为它们创建完整路径,如以下示例所示: >>> os.path.abspath('toaster.txt.') 'C:\\Python27\\Lib\\idlelib\\toaster.txt' >>> os.path.abspath('i\\am\\a\\toaster.txt.') 'C:\\Python27\\Lib\\idlelib\\i\\am\\a\\toaster.txt

我知道abspath可以获取一个文件或一组相对的文件,并通过预先设置当前目录为它们创建完整路径,如以下示例所示:

>>> os.path.abspath('toaster.txt.')
'C:\\Python27\\Lib\\idlelib\\toaster.txt'

>>> os.path.abspath('i\\am\\a\\toaster.txt.')
'C:\\Python27\\Lib\\idlelib\\i\\am\\a\\toaster.txt'
提供的完整路径将被视为绝对路径,而不是在该路径之前:

>>> os.path.abspath('C:\\i\\am\\a\\toaster.txt.')
'C:\\i\\am\\a\\toaster.txt'
>>> os.path.abspath('Y:\\i\\am\\a\\toaster.txt.')
'Y:\\i\\am\\a\\toaster.txt'
我的问题是abspath如何知道如何做到这一点?这是在Windows上,那么它是否在开始时检查“@:”(其中@是任何字母字符)

如果是这样,其他操作系统如何确定?Mac的“/Volumes/”路径作为目录不太容易区分。

参考,Windows 95和Windows NT上的绝对路径如下所示:

# Return whether a path is absolute. 
# Trivial in Posix, harder on Windows. 
# For Windows it is absolute if it starts with a slash or backslash (current 
# volume), or if a pathname after the volume-letter-and-colon or UNC-resource 
# starts with a slash or backslash. 


def isabs(s): 
    """Test whether a path is absolute""" 
    s = splitdrive(s)[1]
    return len(s) > 0 and s[0] in _get_bothseps(s) 
如果
\u getfullpathname
不可用,则由
abspath
调用此函数。不幸的是,我无法找到
\u getfullpathname
的实现

abspath
的实现(如果
\u getfullpathname
不可用):


这是一个非常有用的答案!但是我被你引用的页面的一部分弄糊涂了,它说对于Windows来说,如果它以斜杠或反斜杠(当前卷)开头,它是绝对的。这不是一个相对路径而不是绝对路径吗?我只是在我的Windows PC上检查了它,实际上,
\foo
也指Windows中的绝对路径。准确地说,是当前卷上的绝对路径-如高亮显示的字符串所示。使用命令行,转到
C:
上的某个子文件夹,然后转到
cd\Windows
,自己尝试一下。它会再次把你带到
C:\Windows
。啊哈,现在对我来说这很有意义。非常感谢。
def abspath(path): 
    """Return the absolute version of a path.""" 
    if not isabs(path): 
        if isinstance(path, bytes): 
            cwd = os.getcwdb() 
        else: 
            cwd = os.getcwd() 
        path = join(cwd, path) 
    return normpath(path)