Why not os.path.join use os.path.sep or os.sep?

谁都会走 提交于 2019-12-17 20:29:11

问题


As we know, windows accept both "\" and "/" as separator. But in python, "\" is used. For example, call os.path.join("foo","bar"), 'foo\\bar' will be returned. What's annoying is that there's an escape character, so you cannot just copy the path string and paste to your explorer location bar.

I wonder is there any way to make python use "/" as default separator, I've tried change the value of os.path.sep and os.sep to "/", but os.path.join still use "\\".

what's the right way?

PS:

I just don't understand why python is using "\" as default separator on windows, maybe old version of windows don't support "/"?


回答1:


Why not define a custom display function?

e.g.

def display_path(path):
    return path.replace("\\", "/")

And if you want to substitute str.join for os.path.join, you can just do this (str.join expects a single list, os.path.join expects *args):

join = lambda *args: "/".join(args)

Perhaps better would be to let Python normalize everything, then replace, e.g.:

join = lambda *args: os.path.join(*args).replace("\\", "/")

The only issue with the above might be on posix when there is a space in the file path.

You could then put an if statement at the top of your utils file and define display_path and join as a no-op and as os.path.join respectively if not on Windows.




回答2:


It is all about how Python detects your os:

# in os.py
if 'posix' in _names:
    ...
    import posixpath as path   

elif 'nt' in _names:
    ...
    import ntpath as path

So, on Windows the ntpath module is loaded. If you check the ntpath.py and posixpath.py modules you'd notice that ntpath.join() is a bit more complex and that is also because of the reason you've mentioned: Windows understands / as a path separator.

Bottomline: although you can use posixpath.join() in Windows (as long as the arguments are in POSIX format), I would not recommend doing it.




回答3:


To answer your question as simply as possible, just use posixpath instead of os.path.

So instead of:

from os.path import join
join('foo', 'bar')
# will give you either 'foo/bar' or 'foo\\bar' depending on your OS

Use:

from posixpath import join
join('foo', 'bar')
# will always give you 'foo/bar'



回答4:


I would not recommend doing this.

Note that while windows does accept slash / as path seperator also, it has a different meaning in some contexts.

It's treated as relative path using cd, for example:

Command line:

c:\Users\YourUser> cd /FooBar
c:\FooBar

Here, / substitutes the drive letter.


Also, I don't see a problem at all with copying the strings, since if you print the string, the string is displayed as you wish:

Python interpreter:

>>> import os
>>> print os.path.join("c:\", "foo","bar")
c:\foo\bar
>>>





回答5:


I don't have enough reputation to comment, but the above answer is incorrect.

Windows has a concept of working directory and working drive. / is treated as absolute path on your current working drive, since Windows has no concept of single root. In the above example cd /FooBar goes to C:\foobar because the working drive is C:, not because C: is "root" drive or somehow special.

Here's an example:

C:\Users\user> cd /
C:\> d:
D:\> cd /Users
The system cannot find the path specified.
D:\> mkdir test
D:\> cd test
D:\test> cd c:/Users
D:\test> cd /
D:\> cd test
D:\test> c:
C:\Users\> d:
D:\test> 


来源:https://stackoverflow.com/questions/12086224/why-not-os-path-join-use-os-path-sep-or-os-sep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!