Random Problems

Cousin Stanley cousinstanley at gmail.com
Thu Aug 14 04:19:11 CEST 2008



> Well the othe day I was making a program to make a list 
> of all the songs in certian directorys but I got a problem, 
> only one of the directorys was added to the list. 
> ....
>
  Here's some code .... that illustrates yours ....

import glob

songs  = glob.glob( '/path/to/somewhere/*.mp3' )

asongs = glob.glob( 'path/to/somewhere/else/*.mp3' )

songs.append( asongs )

# repeat a few times appending lists from other dirs

> all goes well but pick awalys is from the first directory 
> but songs awalys includes all the files I want it to. 

  songs.append( asongs ) is appending the entire  asongs  list
  as a single item to the end of the  songs  list, not adding
  each individual song as an entry .... 

  For example ....

>>> l1 = range(  0 ,  5 )
>>> l2 = range(  5 , 10 )
>>> l3 = range( 11 , 15 )
>>>
>>> l1
[0, 1, 2, 3, 4]
>>>
>>> l2
[5, 6, 7, 8, 9]
>>>
>>> l3
[11, 12, 13, 14]
>>>
>>> l1.append( l2 )
>>>
>>> l1
[0, 1, 2, 3, 4, [5, 6, 7, 8, 9]]
>>>
>>> l1.append( l3 )
>>>
>>> l1
[0, 1, 2, 3, 4, [5, 6, 7, 8, 9], [11, 12, 13, 14]]
 
  So, if you have a  lot  of entries in the original  songs  list
  you're only adding a  few  entries to it in the form of another
  list and most likely you didn't run enough  random.choice  tests
  to flush out a  pick  that  turned out to be one of the entire
  asong lists that you added .... 

  You might try something like the following 
  where each tune gets added individually to
  the song pool .... un-tested ....


# -------------------------------------------------------------------

import random
import glob

base_dir = 'c:/Documents and Settings/Admin/My Documents'

list_subdirs = [
    'LimeWire/Saved/*.mp3' ,
    'Downloads/*/*.mp3' ,
    'Downloads/*/*/*.mp3' ,
    'Downloads/*/*/*/*.mp3 ]

song_pool = [ ]             

for this_dir in list_subdirs :

    list_songs = glob.glob( "'%s/%s'" % ( base_dir , this_dir )

    if list_songs :

        for this_song in list_songs :

            song_pool.append( this_song )

npicks = 41

print

for n in range( npicks ) : 

    this_pick = random.choice( song_pool )

    print '   ' , this_pick

# ------------------------------------------------------------------- 
   

-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona




More information about the Python-list mailing list