Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mixed slashes with os.path.join on windows

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 want to go cross-platform. But when I use it I get mixed slashes:

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

Is this correct? Should I check and correct it afterward or there is a better way?

Thanks

EDIT: I also get mixed slashes when asking for paths

import sys for item in sys.path:     print item  # Result: C:\Program Files\Autodesk\Maya2013.5\bin C:\Program Files\Autodesk\Maya2013.5\mentalray\scripts\AETemplates C:\Program Files\Autodesk\Maya2013.5\Python C:\Program Files\Autodesk\Maya2013.5\Python\lib\site-packages C:\Program Files\Autodesk\Maya2013.5\bin\python26.zip\lib-tk C:/Users/nookie/Documents/maya/2013.5-x64/prefs/scripts C:/Users/nookie/Documents/maya/2013.5-x64/scripts C:/Users/nookie/Documents/maya/scripts C:\Program Files\Nuke7.0v4\lib\site-packages C:\Program Files\Nuke7.0v4/plugins/modules 
like image 911
nookie Avatar asked May 02 '13 08:05

nookie


People also ask

Does os path join work for Windows?

This works on any platform where users have a home directory, including Linux, Mac OS X, and Windows. The returned path does not have a trailing slash, but the os. path. join() function doesn't mind.

How do you use forward slash in os path join?

The os. path. join() automatically adds forward slashes (“/”) into the pathname when needed.

Does os path join add trailing slash?

os. path. join(path, '') will add the trailing slash if it's not already there.

What is os path join?

os. path. join combines path names into one complete path. This means that you can merge multiple parts of a path into one, instead of hard-coding every path name manually.


2 Answers

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.

like image 198
Maximus Avatar answered Sep 20 '22 15:09

Maximus


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.

like image 27
pyrocumulus Avatar answered Sep 18 '22 15:09

pyrocumulus