How to check if a path is absolute path or relative path in a cross-platform way with Python?

Viewed 81246

UNIX absolute path starts with '/', whereas Windows starts with alphabet 'C:' or '\'. Does python have a standard function to check if a path is absolute or relative?

9 Answers

From python 3.4 pathlib is available.

In [1]: from pathlib import Path

In [2]: Path('..').is_absolute()
Out[2]: False

In [3]: Path('C:/').is_absolute()
Out[3]: True

In [4]: Path('..').resolve()
Out[4]: WindowsPath('C:/the/complete/path')

In [5]: Path('C:/').resolve()
Out[5]: WindowsPath('C:/')

@Zbyl Under DOS, since the resulting path does not change for different current directories, it is arguably an absolute path. I say arguably because the resulting path does change relative to the current drive!

This is left over from DOS, which has a different current directory per drive.

By selecting a different current drive, you implicitly change the current directory. For example, I just did "CD" (the DOS equiv of pwd)

* CD
C:\Windows\System32

Then changed the current drive:

* t:
T:\

This is correct if unexpected. Since I cannot remember 26 current directories, I never use this.

Also note that CD is "broken":

T:\ * cd c:\Windows 
T:\

The current directory (on t:) is not changed, but it is changed on C: We just have to change the current drive to see that:

T:\ * c:
c:\Windows *

I always use pushd to change drive & directory:

T:\ * pushd c:\Windows\assembly                                                                                                                                                                                                     
c:\Windows\assembly * 

Since network shares don't have a volume, there is no obvious way of setting a current directory. Pushd knows how. If you do something like

pushd \\myhost\myshare\folder

DOS/Windows maps the share to the last available drive letter, typically Z. Then change to the folder you specified. This is particularly important for batch files that need to run with the current directory set to the batch file location. For this I start many batch files off with:

SETLOCAL EnableExtensions
pushd "%~dp0"

SETLOCAL ensures the new mapped volume is unmapped at the end of the batch file. Otherwise you would quickly run out of volume letters

You can use the os or the pathlib libraries.

Using os

>>> from os.path import isabs
>>> isabc("./")
False
>>> isabc("C:/")
True

Using pathlib

>>> from pathlib import Path
>>> Path("./").is_absolute()
False
>>> Path("C:/").is_absolute()
True

But as @Shoham says in his answer https://stackoverflow.com/a/41846670/14475596
Actually I think none of the above answers addressed the real issue: cross-platform paths. What os.path does is load the OS dependent version of 'path' library. so the solution is to explicitly load the relevant (OS) path library:

>>> import ntpath
>>> import posixpath
>>>
>>> ntpath.isabs("Z:/a/b/c../../H/I/J.txt")
>>> True
>>> posixpath.isabs("Z:/a/b/c../../H/I/J.txt")
>>> False
Related