推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
XIVN1987
V2EX  ›  Python

怎么让 os.path.join 在 windows 下使用'/'而不是'\\'?

  •  
  •   XIVN1987 · Dec 2, 2019 · 6727 views
    This topic created in 2533 days ago, the information mentioned may be changed or developed.
    windows 系统下执行:os.path.join('/abc/def', 'mnk')

    结果是:'/abc/def\\mnk'

    我想要的结果是:'/abc/def/mnk'

    请问如何操作?
    21 replies  •  2019-12-02 15:46:29 +08:00
    Chase2E
    Chase2E
    Dec 2, 2019
    1
    同问
    itskingname
    itskingname
    Dec 2, 2019
    2
    那么你就不要用 os.path.join,改成

    ```
    path = ['/abc/def', 'mnk']
    result = '/'.join(path)
    print(result)
    ```
    wangyzj
    wangyzj
    Dec 2, 2019
    3
    别用 os.path
    XIVN1987
    XIVN1987
    OP
    Dec 2, 2019
    4
    @itskingname

    多谢,,麻烦的方式有很多,,比如 os.path.join('/abc/def', 'mnk').replace('\\', '/')

    提问是想看看有没有简单的方法
    wuwukai007
    wuwukai007
    Dec 2, 2019 via Android
    5
    os.path.join(abc,def,mnk)
    U7Q5tLAex2FI0o0g
    U7Q5tLAex2FI0o0g
    Dec 2, 2019
    6
    你本来就不应该把 /abc/def 写死,代码里写死路径分隔符是不好的习惯
    不知道 python 有没有定义常量,PHP 有一个 DIRECTORY_SEPARATOR https://www.php.net/manual/en/dir.constants.php
    hhhsuan
    hhhsuan
    Dec 2, 2019❤️1
    7
    用 pathlib
    Trim21
    Trim21
    Dec 2, 2019 via Android
    8
    @littleylv 好像是 os.sep 还是 os.path.sep 来着
    Trim21
    Trim21
    Dec 2, 2019 via Android
    9
    手头没电脑,没法实际测试。你看看 urllib 里面的 join 行不行
    ClericPy
    ClericPy
    Dec 2, 2019
    10
    pathlib 的 Path 对象有个 as_posix 就可以了, 何必非纠结 os.path 呢
    Cooky
    Cooky
    Dec 2, 2019 via Android❤️1
    11
    pathlib ?
    hhhsuan
    hhhsuan
    Dec 2, 2019❤️2
    12
    from pathlib import PurePosixPath

    p = PurePosixPath("/abc/def")
    p = p.joinpath("mnk")
    print(p)

    > /abc/def/mnk
    Procumbens
    Procumbens
    Dec 2, 2019❤️3
    13
    import posixpath
    posixpath.join()

    From [os.path documentation]( https://docs.python.org/3/library/os.path.html):
    Since different operating systems have different path name conventions, there are several versions of this module in the 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
    XIVN1987
    XIVN1987
    OP
    Dec 2, 2019
    14
    @Procumbens

    感谢,,感觉这个方法最好
    XIVN1987
    XIVN1987
    OP
    Dec 2, 2019
    15
    感觉 @Procumbens 提出的方法最简洁

    只需要 import posixpath as path

    然后把 os.path.join('/abc/def', 'mnk') 都替换成 path.join('abc/def', 'mnk') 就可以了,,
    yuankui
    yuankui
    Dec 2, 2019
    16
    os.path 不就是为了做跨平台兼容的吗? window 用 /路径分隔符有啥意义?
    yucongo
    yucongo
    Dec 2, 2019
    17
    from pathlib import Path

    Path('/abc/def', 'mnk').as_posix() # ->'/abc/def/mnk'
    XIVN1987
    XIVN1987
    OP
    Dec 2, 2019
    18
    @yuankui

    如果合成的路径是要发到远程主机上使用的话就有意义了
    starix
    starix
    Dec 2, 2019
    19
    os.sep
    BlueSummer8
    BlueSummer8
    Dec 2, 2019
    20
    用 str 的 join 怎样,'/'.join(('aaa', 'bbb'))
    JCZ2MkKb5S8ZX9pq
    JCZ2MkKb5S8ZX9pq
    Dec 2, 2019
    21
    我发到远程主机的,所以碰到过类似问题。
    可以考虑自己写一个 path_join 方法,这样里面套什么模块或逻辑都行,以后改起来也方便点只改一处。
    我是写了一个 format_path,比如有些情况第一个斜杠也需要处理,等等。然后把 replace 啥的都塞里面了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1521 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 52ms · UTC 09:42 · PVG 17:42 · LAX 01:42 · JFK 04:42
    ♥ Do have faith in what you're doing.