在 Windows 上使用 os.path 进行 POSIX 路径操作
Using os.path for POSIX Path Operations on Windows
我在 Windows 上使用 Paramiko 访问远程 SFTP 服务器。我需要对 os.path.join
、os.path.commonprefix
等远程路径进行一些处理。由于我的主机平台是 Windows,因此所有路径操作都带有 \
分隔符,但我需要 POSIX 样式的路径。
有什么方法可以在 Windows 上使用 Python 内置 POSIX 路径操作吗?
正如 documentation(第 2 个注释)
中所指出的
... you can also import and use the individual modules if you want to
manipulate a path that is always in one of the different formats. They
all have the same interface:
posixpath
for UNIX-style paths
ntpath
for Windows paths
因此您可以导入 posixpath
并将其用作 os.path
>>> import posixpath
>>> posixpath.join
<function join at 0x025C4170>
>>> posixpath.join('a','b')
'a/b'
>>> posixpath.commonprefix
<function commonprefix at 0x00578030>
在我看来pathlib.PurePosixPath is the way to go (as already commented by phoenix).
例如:
>>> import pathlib
>>> pathlib.PurePosixPath('a', 'b')
PurePosixPath('a/b')
>>> str(pathlib.PurePosixPath('a', 'b'))
'a/b'
来自相关文档:
class pathlib.PurePosixPath(*pathsegments)
A subclass of PurePath, this path flavour represents non-Windows filesystem paths:
>>> PurePosixPath('/etc')
PurePosixPath('/etc')
pathsegments is specified similarly to PurePath.
class pathlib.PureWindowsPath(*pathsegments)
A subclass of PurePath, this path flavour represents Windows filesystem paths:
>>> PureWindowsPath('c:/Program Files/')
PureWindowsPath('c:/Program Files')
pathsegments is specified similarly to PurePath.
Regardless of the system you’re running on, you can instantiate all of
these classes, since they don’t provide any operation that does system
calls.
我在 Windows 上使用 Paramiko 访问远程 SFTP 服务器。我需要对 os.path.join
、os.path.commonprefix
等远程路径进行一些处理。由于我的主机平台是 Windows,因此所有路径操作都带有 \
分隔符,但我需要 POSIX 样式的路径。
有什么方法可以在 Windows 上使用 Python 内置 POSIX 路径操作吗?
正如 documentation(第 2 个注释)
中所指出的... you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats. They all have the same interface:
posixpath
for UNIX-style pathsntpath
for Windows paths
因此您可以导入 posixpath
并将其用作 os.path
>>> import posixpath
>>> posixpath.join
<function join at 0x025C4170>
>>> posixpath.join('a','b')
'a/b'
>>> posixpath.commonprefix
<function commonprefix at 0x00578030>
在我看来pathlib.PurePosixPath is the way to go (as already commented by phoenix).
例如:
>>> import pathlib
>>> pathlib.PurePosixPath('a', 'b')
PurePosixPath('a/b')
>>> str(pathlib.PurePosixPath('a', 'b'))
'a/b'
来自相关文档:
class pathlib.PurePosixPath(*pathsegments)
A subclass of PurePath, this path flavour represents non-Windows filesystem paths:
>>> PurePosixPath('/etc') PurePosixPath('/etc')
pathsegments is specified similarly to PurePath.
class pathlib.PureWindowsPath(*pathsegments)
A subclass of PurePath, this path flavour represents Windows filesystem paths:
>>> PureWindowsPath('c:/Program Files/') PureWindowsPath('c:/Program Files')
pathsegments is specified similarly to PurePath.
Regardless of the system you’re running on, you can instantiate all of these classes, since they don’t provide any operation that does system calls.