简体   繁体   English

如何使用Python跨平台检查路径是绝对路径还是相对路径?

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

UNIX absolute path starts with '/', whereas Windows starts with alphabet 'C:' or '\'. UNIX 绝对路径以“/”开头,而 Windows 以字母“C:”或“\”开头。 Does python have a standard function to check if a path is absolute or relative? python 是否有标准的 function 来检查路径是绝对路径还是相对路径?

os.path.isabs returns True if the path is absolute, False if not.如果路径是绝对路径, os.path.isabs返回True ,否则返回False The documentation says it works in windows (I can confirm it works in Linux personally).文档说它可以在 Windows 中运行(我可以亲自确认它在 Linux 中运行)。

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 :如果您真正想要的是绝对路径,请不要费心检查它是否是,只需获取abspath

import os

print os.path.abspath('.')

From python 3.4 pathlib is available.python 3.4 pathlib可用。

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

使用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. os.path 所做的是加载依赖于操作系统的“路径”库版本。 so the solution is to explicitly load the relevant (OS) path library:所以解决方案是显式加载相关(OS)路径库:

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

os.path模块将满足您的所有需求。

@Zbyl Under DOS, since the resulting path does not change for different current directories, it is arguably an absolute path. @Zbyl 在DOS下,由于当前不同目录的结果路径不会改变,因此可以说是绝对路径。 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.这是 DOS 遗留下来的,每个驱动器都有不同的当前目录。

By selecting a different current drive , you implicitly change the current directory.通过选择不同的当前驱动器,您可以隐式更改当前目录。 For example, I just did "CD" (the DOS equiv of pwd)例如,我刚刚做了“CD”(DOS 等同于 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.因为我不记得 26 个当前目录,所以我从不使用它。

Also note that CD is "broken":另请注意,CD 已“损坏”:

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: 上改变了:我们只需更改当前驱动器即可看到:

T:\ * c:
c:\Windows *

I always use pushd to change drive & directory:我总是使用 pushd 来更改驱动器和目录:

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. Pushd知道怎么做。 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. DOS/Windows 将共享映射到最后一个可用的驱动器号,通常是 Z。然后切换到您指定的文件夹。 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. SETLOCAL 确保新映射的卷在批处理文件末尾未映射。 Otherwise you would quickly run out of volume letters否则你会很快用完卷字母

You can use the os or the pathlib libraries.您可以使用ospathlib库。

Using os使用os

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

Using pathlib使用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但正如@Shoham 在他的回答中所说的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. os.path 所做的是加载依赖于操作系统的“路径”库版本。 so the solution is to explicitly load the relevant (OS) path library:所以解决方案是显式加载相关(OS)路径库:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM