开发者

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

UNIX absol开发者_如何学Cute 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?


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)


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('.')


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:/')


Use os.path.isabs.


import os.path

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

os.path.isabs('user')
False


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


@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


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜