Mixed Slashes with Os.Path.Join on Windows

mixed slashes with os.path.join on windows

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.

Resolving mixed slashes from sys.path and os.path.join

How about using os.path.normpath()?

>>> import os
>>> os.path.normpath(r'c:\my/path\to/something.py')
'c:\\my\\path\\to\\something.py'

Also worth mentioning: the Windows path API doesn't care whether forward or back slashes are used. Usually it's the fault of program that doesn't handle the slashing properly. For example, in python:

with open(r'c:/path/to/my/file.py') as f:
print f.read()

will work.

Python os.path.join on Windows

Windows has a concept of current directory for each drive. Because of that, "c:sourcedir" means "sourcedir" inside the current C: directory, and you'll need to specify an absolute directory.

Any of these should work and give the same result, but I don't have a Windows VM fired up at the moment to double check:

"c:/sourcedir"
os.path.join("/", "c:", "sourcedir")
os.path.join("c:/", "sourcedir")

python os.walk displays mixed windows and unix paths

There is no actual problem here. Windows supports two path separators; the forward and backward slashes are both valid and supported, even when mixed. One is the os.sep (\), and the other the os.altsep character (/).

os.path.join() user os.sep to join paths, but won't replace os.altsep in the input paths. os.walk() just uses os.path.join() to build the first element of each (path, files, directories) tuple it generates

If this bothers you, normalise your paths, using the os.path.normpath() function:

On Windows, it converts forward slashes to backward slashes.

So normalise the path passed to os.walk():

for paths, subdirs, files in os.walk(os.path.normpath(start_dir), topdown=True):
for file in files:
full_path = os.path.join(paths, file)
print(full_path)

or normalise the paths generated in the loop:

for paths, subdirs, files in os.walk(start_dir, topdown=True):
for file in files:
full_path = os.path.join(paths, file)
normalised = os.path.normpath(full_path)
print(normalised)

or normalise the input string:

os.path.join generates 4 backslashes per folder

These backslashes are side effect of the fact that they're backslashes. Gobbledygook :-)

In strings, the backslash to mean backslash is often preceded by backslash in CLI, otherwise in precedes special character synonym, like \n for newline, \t for tab. From my experience the number of these backslashes does not cause problems. You can always try to normalize the path in string by os.path.normpath().

This problem is certainly Windows specific.

os.path.join - how to cope with absolute path

"If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component." applies here: STATIC_URL is an absolute path because it starts with /, so BASE_DIR is dropped.

Drop the leading / else dirname thinks that STATIC_URL is absolute and keeps only that.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = 'static/'


Related Topics



Leave a reply



Submit