[Tutor] slashes in paths

Jim Mooney cybervigilante at gmail.com
Sat Jul 20 20:24:48 CEST 2013


I was looking at os.path.join, which is supposed to join paths
intelligently. I've used it but this time I  actually looked at the
path for this:

#Using Python 2.7 on Win 7
from __future__ import division, print_function
import os

soundpath = 'c:/python27/jimprogs/wav'
soundlist = os.listdir(soundpath)
soundfile13 = os.path.join(soundpath, soundlist[13])

The result is:

>>> soundfile13
'c:/python27/jimprogs/wav\\bicycle_bell.wav'
>>>

with single forward slashes mixed with a double backslash

it comes out even worse if I print it

c:/python27/jimprogs/wav\bicycle_bell.wav  - no double backslash,
which could create a mess if someone copied that and tried it.

Oddly, the mixed path does work for playing sounds,

>>> import winsound
>>> winsound.PlaySound('c:/python27/jimprogs/wav\\bicycle_bell.wav',winsound.SND_FILENAME)
>>>
That rings a bell.

But it just doesn't look right. If forward slash is a Python
convention instead of double backslash, it should follow through. I
can see where you could get in trouble with it.

It does work right if I end the path with a / - i.e. - soundpath =
'c:/python27/jimprogs/wav/' but that's not required. Ending with a
forward slash or  not seems to work either way, although I'm not sure
about Linux.

-- 
Jim

The universe is made up of patches that are only woven together as
they are accessed. Sometimes a stitch is dropped and something really
weird happens. Only nobody will ever believe you


More information about the Tutor mailing list