Raw Strings (I Think)

Fredrik Lundh fredrik at pythonware.com
Wed Jul 23 22:43:31 CEST 2008

Lanny wrote:

>> No, it doesn't. Instead of guessing what the cause might be, please show
>> us your code and show us the error message you're getting, so that we can 
>> determine what the cause really is.
> Ok, sorry. Heres my code:
> import glob
> import random
> import os
> songs = glob.glob('C:\###\###\###\*.mp3')
> pick = random.choice(songs)
> os.system(pick)
> And yes, I know there are better ways of randomly selecting
> a .mp3 file to play but I don't care. 

my guess is that the real problem is that you get back filenames with 
spaces in them, which gets treated as multiple arguments by os.system.

using os.startfile will fix this:

 >>> import glob, os, random
 >>> file = random.choice(glob.glob("\\music\\*.mp3"))
 >>> file
'\\music\\Madrugada - Grit - 05 - Seven Seconds.mp3'
 >>> print file
\music\Madrugada - Grit - 05 - Seven Seconds.mp3
 >>> os.system(file)
'\music\Madrugada' is not recognized as an internal or external command, 
operable program or batch file.
 >>> os.startfile(file)
... music starts playing ...


More information about the Python-list mailing list