194

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?

Diamond
  • 598
  • 5
  • 15
prosseek
  • 182,215
  • 215
  • 566
  • 871

9 Answers9

278

os.path.isabs returns True if the path is absolute, False if not. The documentation says it works in windows (I can confirm it works in Linux personally).

os.path.isabs(my_path)
Donald Miner
  • 38,889
  • 8
  • 95
  • 118
  • 3
    This function is not cross-platform. On Unix `os.path.isabs('c:\\')` returns False. – anatoly techtonik Nov 22 '12 at 07:27
  • 88
    Rotfl, and it should return False, since C:\\ is not an absolute path in Unix system. Absolute paths on unix platforms starts with "/", like "/var/custApp/" etc. :) – Marek Lewandowski May 03 '13 at 09:07
  • 41
    @techtonik To make it perfectly clear to anyone else who might find this: `c:\\` is a perfectly valid file/directory name in unix. So, it would really be a relative path on a unix system. Hence, the function is cross-platform. Because, it takes the specialties of Windows and Unix into account. – Lemming Nov 20 '13 at 14:52
  • 6
    Exactly. It doesn't behave the same on different platforms, it gives the correct answer for the current platform. – Kevin Cox Nov 08 '16 at 12:41
  • 1
    Not answering the question. To have cross-platform rules use 'ntpath' or 'posixpath' instead of 'os.path' – Shoham Jan 25 '17 at 08:30
51

And if what you really want is the absolute path, don't bother checking to see if it is, just get the abspath:

import os

print(os.path.abspath('.'))  # relative paths become absolute
print(os.path.abspath('C:\Users'))  # absolute paths will remain absolute
xjcl
  • 12,848
  • 6
  • 67
  • 89
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • 8
    it may not have answered the original question, but exactly what i was looking for without realizing it. thanks! – mephisto Apr 11 '13 at 08:22
31

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:/')
Praveen
  • 8,945
  • 4
  • 31
  • 49
  • 3
    This answer actually work on Windows..`Path('\tmp').is_absolute()` gives correctly `False`, while `os.path.isabs('\tmp')` gives incorrectly `True`. (Some would argue that `\tmp` is an absolute path on Windows, but that's only true for a very useless definition of an *absolute path*.) – Zbyl Jun 22 '20 at 13:05
  • Weird. Path('C:/').is_absolute() return False for me: Python 3.6.9 (default, Jul 17 2020, 12:50:27) >>> from pathlib import Path >>> Path('..').is_absolute() False >>> Path('C:/').is_absolute() False >>> – ZeZNiQ Oct 04 '21 at 03:07
  • @ZeZNiQ You forgot to mention your OS. It is only True on Windows, as it should. – Robert Siemer Nov 28 '22 at 09:13
18

Use os.path.isabs.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
9
import os.path

os.path.isabs('/home/user')
True

os.path.isabs('user')
False
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
Alex Bliskovsky
  • 5,973
  • 7
  • 32
  • 41
4

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
Shoham
  • 1,079
  • 1
  • 12
  • 17
0

@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

Andrew Dennison
  • 1,069
  • 11
  • 9
  • How does this answer the question? – miken32 Jul 13 '22 at 19:08
  • @miken32 The current directory and Relative paths in Windows may not resolve in a cross-platform compatible way. Only you can decide if the DOS/Windows concepts of current drive and current directory and how Windows resolves relative paths is important to you I am not sure what you are asking. If my answer is completely irrelevant, flag it. If my answer is unclear, incorrect, long-winded say so, and I will try to improve it as you suggest. – Andrew Dennison Aug 11 '22 at 14:55
  • The question was "Does Python have a standard function to check if a path is absolute or relative" and it was answered more than a decade ago to everyone's satisfaction. Your answer, while possibly an interesting tangent, seems to have nothing to do with Python. – miken32 Aug 11 '22 at 14:58
0

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
Kh4lid MD
  • 125
  • 1
  • 2
  • 10
-3

another way if you are not in current working directory, kinda dirty but it works for me.

import re
path = 'my/relative/path'
# path = '..my/relative/path'
# path = './my/relative/path'

pattern = r'([a-zA-Z0-9]|[.])+/'
is_ralative = bool(pattern)
Mahendra
  • 335
  • 1
  • 5
  • 12