mixed slashes with os.path.join on windows

前端 未结 7 1343
一生所求
一生所求 2020-11-27 04:39

I tend to use only forward slashes for paths (\'/\') and python is happy with it also on windows. In the description of os.path.join it says that is the correct way if you w

相关标签:
7条回答
  • 2020-11-27 04:45

    os adds slashes for you and makes sure not to duplicate slashes so omit them in your strings

    import os
    
    # Don't add your own slashes
    a = 'C:'
    b = 'myFirstDirectory' 
    c = 'mySecondDirectory'
    d = 'myThirdDirectory'
    e = 'myExecutable.exe'
    
    print os.path.join(a, b, c, d, e)
    C:\myFirstDirectory\mySecondDirectory\myThirdDirectory\myExecutable.exe
    

    Additional:

    I'm unsure as to why you have mixed slashes in your sys path (have you used a linux os to add some folders?) but try checking

    print os.path.isdir(os.path.join('C:','Users','nookie')).

    If this is True then os works for your mixed slashes.

    Either way, I would avoid hard-coding directory names into your program. Your sys.path for loop is a safe way to pull out these directories. You can then use some string methods, or regex to pick the desired folder.

    0 讨论(0)
  • 2020-11-27 04:59

    You are now providing some of the slashes yourself and letting os.path.join pick others. It's better to let python pick all of them or provide them all yourself. Python uses backslashes for the latter part of the path, because backslashes are the default on Windows.

    import os
    
    a = 'c:' # removed slash
    b = 'myFirstDirectory' # removed slash
    c = 'mySecondDirectory'
    d = 'myThirdDirectory'
    e = 'myExecutable.exe'
    
    print os.path.join(a + os.sep, b, c, d, e)
    

    I haven't tested this, but I hope this helps. It's more common to have a base path and only having to join one other element, mostly files.

    By the way; you can use os.sep for those moments you want to have the best separator for the operating system python is running on.

    Edit: as dash-tom-bang states, apparently for Windows you do need to include a separator for the root of the path. Otherwise you create a relative path instead of an absolute one.

    0 讨论(0)
  • 2020-11-27 05:00

    If for any reason you need to provide the paths yourself and you have using anything above python 3.4 you can use pathlib

    from pathlib import Path, PurePosixPath
    
    a = PurePosixPath('c:/')
    b = PurePosixPath('myFirstDirectory/')
    c = 'mySecondDirectory'
    d = 'myThirdDirectory'
    e = 'myExecutable.exe'
    
    print(a / b / c / d / e)
    
    # Result
    c:/myFirstDirectory/mySecondDirectory/myThirdDirectory/myExecutable.exe
    

    I used this when I needed a user to provide the location of an assets directory and my code was looking up using windows path strings

    In [1]: from pathlib import Path, PureWindowsPath
    In [2]: USER_ASSETS_DIR = Path('/asset/dir') # user provides this form environment variable
    In [3]: SPECIFIC_ASSET = PureWindowsPath('some\\asset')
    In [4]: USER_ASSETS_DIR / SPECIFIC_ASSET
    
    Out[4]: PosixPath('/asset/dir/some/asset')
    
    0 讨论(0)
  • 2020-11-27 05:01

    try using abspath (using python 3)

    import os
    
    a = 'c:/'
    b = 'myFirstDirectory/'
    c = 'mySecondDirectory'
    d = 'myThirdDirectory'
    e = 'myExecutable.exe'
    
    
    print(os.path.abspath(os.path.join(a, b, c, d, e)))
    

    OUTPUT:

    c:\myFirstDirectory\mySecondDirectory\myThirdDirectory\myExecutable.exe

    Process finished with exit code 0

    0 讨论(0)
  • 2020-11-27 05:01

    EDIT based on comment: path = os.path.normpath(path)

    My previous answer lacks the capability of handling escape characters and thus should not be used:

    • First, convert the path to an array of folders and file name.
    • Second, glue them back together using the correct symbol.

      import os   
      path = 'c:\www\app\my/folder/file.php'
      # split the path to parts by either slash symbol:
      path = re.compile(r"[\/]").split(path)
      # join the path using the correct slash symbol:
      path = os.path.join(*path)
      
    0 讨论(0)
  • You can use .replace() after path.join() to ensure the slashes are correct:

    # .replace() all backslashes with forwardslashes
    print os.path.join(a, b, c, d, e).replace("\\","/")
    

    This gives the output:

    c:/myFirstDirectory/mySecondDirectory/myThirdDirectory/myExecutable.exe
    

    As @sharpcloud suggested, it would be better to remove the slashes from your input strings, however this is an alternative.

    0 讨论(0)
提交回复
热议问题