简体   繁体   中英

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:

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? Ie I want to see if there is anything similar to: os.path.setos('Linux')

os.path is a different module depending on your OS. As documented in the os.path documentation, you can explicitly import the version for Unix-style paths:

import posixpath

The other versions are ntpath for Windows paths, macpath for old-style MacOS paths, and os2emxpath for OS/2 EMX paths.

Since different operating systems have different path name conventions, there are several versions of this module in python standard library. The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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