6

I tried the code as below, attempting to change directory separator to forward slash / but still stuck in the backslash \. The documentation says the function joins paths using directory separator os.sep, but this didn't work in my case.

import os
os.sep = '/'
print(os.sep)
print(os.path.join('.', 'path'))
Nicholas
  • 2,560
  • 2
  • 31
  • 58
  • 1
    Maybe you want `'/'.join(['.', 'path'])` instead? For most cases (not all, however, so beware) you will get the same output as if your `sep` was set as you had hoped. – jedwards Jul 31 '16 at 02:17
  • Why do you want to do that? Is backslash actually wrong for your operating system? – Stefan Pochmann Jul 31 '16 at 02:28

5 Answers5

9

I think this answers the questions why Python uses a particular separator.

That said, you can use the Pathlib module to construct your paths and specify whether you want a Posix or Windows path.

Example:

from pathlib import PurePosixPath, PureWindowsPath

print(PurePosixPath('some', 'silly', 'long', 'path'))
>> some/silly/long/path

print(PureWindowsPath('some', 'silly', 'long', 'path'))
>> some\silly\long\path

Make sure you use the pure version of PosixPath and WindowsPath. If you're trying to use WindowsPath on a Posix system, you'll get the following error:

NotImplementedError: cannot instantiate 'WindowsPath' on your system

This is also specified in the docs:

If you want to manipulate Windows paths on a Unix machine (or vice versa). You cannot instantiate a WindowsPath when running on Unix, but you can instantiate PureWindowsPath.

Community
  • 1
  • 1
DocZerø
  • 8,037
  • 11
  • 38
  • 66
1

You can take a look at the source code for the different operating systems. For example, the Mac version is:

def join(s, *p):
    path = s 
    for t in p:
        if (not s) or isabs(t):
            path = t 
            continue
        if t[:1] == ':':
            t = t[1:]
        if ':' not in path:
            path = ':' + path
        if path[-1:] != ':':
            path = path + ':' 
        path = path + t 
    return path

You can see that it is placed directly into the function. It does not depend on os.sep. Each Python installation includes the os.path functions for every operating system. They are available in the Python directory under macpath.py, ntpath.py, and posixpath.py. If you look at each one, you will notice that the posixpath module has what you want:

import posixpath

print(posixpath.join('.', 'path'))
zondo
  • 19,901
  • 8
  • 44
  • 83
0

You can replace function in os.path, with self own:

import os
path = "public\\INSTALL\\"
print("Initial unmodified join return: '%s'" % os.path.join('.', path) )
native_os_path_join = os.path.join
def modified_join(*args, **kwargs):
    return native_os_path_join(*args, **kwargs).replace('\\', '/')
os.path.join = modified_join
print("Modified join return: '%s'" % os.path.join('.', path) )

Output:

Initial unmodified join return: '.\public\INSTALL\'
Modified join return: './public/INSTALL/'
andreytata
  • 19
  • 3
0

So to sum up we can simply do the following if for some reason os.path.join() is not working properly

   os.sep.join([path,name])
0

With pathlib, you can call .as_posix() on a constructed Path to get its string representation with forward slashes as the separator.

Examples:

>>> from pathlib import Path
>>> Path('dirA', 'dirB', 'file.txt').as_posix()
'dirA/dirB/file.txt'
>>> from pathlib import PureWindowsPath
>>> PureWindowsPath('dirA\\dirB', 'file.txt').as_posix()
'dirA/dirB/file.txt'
Unmitigated
  • 76,500
  • 11
  • 62
  • 80