简体   繁体   English

有什么办法可以为os.path指定平台吗?

[英]Is there any way I can specify the platform for os.path?

I am under Windows, but I need to process some unix-style path, such as: 我在Windows下,但我需要处理一些unix风格的路径,例如:

In[4]: os.path.abspath('/prj/vl')
Out[4]: 'C:\\prj\\vl'

which is not the correct output I want. 这不是我想要的正确输出。 So I am wondering if there is a way that I can force os.path to work as it is under Unix? 所以我想知道是否有一种方法可以强制os.path在Unix下工作? Ie I want to see if there is anything similar to: os.path.setos('Linux') 即我想看看是否有类似的东西: os.path.setos('Linux')

os.path is a different module depending on your OS. os.path是一个不同的模块,具体取决于您的操作系统。 As documented in the os.path documentation, you can explicitly import the version for Unix-style paths: os.path 文档中所述,您可以显式导入Unix样式路径的版本:

import posixpath

The other versions are ntpath for Windows paths, macpath for old-style MacOS paths, and os2emxpath for OS/2 EMX paths. 其他版本ntpath的Windows路径, macpath适用于旧的MacOS的路径,并os2emxpath用于OS / 2 EMX路径。

Since different operating systems have different path name conventions, there are several versions of this module in python standard library. 由于不同的操作系统具有不同的路径名约定,因此python标准库中有多个版本的此模块。 The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. os.path模块始终是适用于Python运行的操作系统的路径模块,因此可用于本地路径。 However, 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
`macpath` for old-style MacOS paths
`os2emxpath` for OS/2 EMX paths

reference: https://docs.python.org/2/library/os.path.html 参考: https//docs.python.org/2/library/os.path.html

您可以使用posixpath而不是os.path - 实际上, os.path使用posixpathntpath具体取决于系统。

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

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