[Python] Force "os" or "posixpath" to use forward slashes?

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    To get paths that use forward slash as a path separator, Google AI says to use the posixpath module instead of the os module. However, I find that it still uses backslashes:

    >>> import os
    >>> os.path.abspath('LoadJSON.py')
    'C:\\cygwin64\\home\\User.Name\\prj\\SomeProject\\Some-Subfolder\\LoadJSON.py'

    >>> import posixpath as pp
    >>> pp.abspath('LoadJSON.py')
    'C:\\cygwin64\\home\\User.Name\\prj\\SomeProject\\Some-Subfolder/LoadJSON.py'


    If possible, I would like to avoid littering my code with string replacements. Is it possible to get paths that use forward slashe by default?

    I am using:

    • Spyder version: 6.0.5 (conda)
    • Python version: 3.9.18 64-bit
    • Qt version: 5.15.2
    • PyQt5 version: 5.15.10
    • Operating System: Windows-10-10.0.26100-SP0

    Continue reading...

Compartilhe esta Página