Random Problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lanny

    Random Problems

    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. Heres my code:

    import random
    import os
    import glob

    songs = glob.glob('C:\D ocuments and Settings\Admin\ My
    Documents\LimeW ire\Saved\*.mp3 ')
    asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
    Documents\Downl oads\*\*.mp3')
    songs.append(as ongs)
    asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
    Documents\Downl oads\*\*\*.mp3' )
    songs.append(as ongs)
    asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
    Documents\Downl oads\*\*\*\*.mp 3')
    songs.append(as ongs)
    pick = random.choice(s ongs)

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



    -- Posted on news://freenews.netfront.net - Complaints to news@netfront.n et --
  • Larry Bates

    #2
    Re: Random Problems

    Lanny wrote:
    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. Heres my code:
    >
    import random
    import os
    import glob
    >
    songs = glob.glob('C:\D ocuments and Settings\Admin\ My
    Documents\LimeW ire\Saved\*.mp3 ')
    asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
    Documents\Downl oads\*\*.mp3')
    songs.append(as ongs)
    asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
    Documents\Downl oads\*\*\*.mp3' )
    songs.append(as ongs)
    asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
    Documents\Downl oads\*\*\*\*.mp 3')
    songs.append(as ongs)
    pick = random.choice(s ongs)
    >
    all goes well but pick awalys is from the first directory but songs awalys
    includes all the files I want it to. Im baffaled.
    >
    >
    >
    >
    -- Posted on news://freenews.netfront.net - Complaints to news@netfront.n et --
    >
    1) You need to either use raw string for your pathnames or use forward slashes.
    This is because backslash is an escape character to Python and if you get any
    legal escaped sequence (like \n, \t, etc) it won't work as expected.

    songs = glob.glob(r'C:\ Documents and Settings\Admin\ My
    Documents\LimeW ire\Saved\*.mp3 ')

    or

    songs = glob.glob('C:/Documents and Settings/Admin/My
    Documents/LimeWire/Saved/*.mp3')

    Yes, forward slashes work just fine on windows.

    2) When you have a list (songs) and append another list (asongs) you don't get a
    combined list, you get a list with the last element being the second list.

    example:
    >>songs = [1,2,3]
    >>asongs = [4,5,6]
    >>songs.append( asongs)
    >>songs
    [1, 2, 3, [4, 5, 6]]
    >>>
    What you wanted was songs.extend(as ongs). BTW-Inserting a couple of print
    statements would have shown you this problem pretty quickly.

    -Larry

    Comment

    • Lanny

      #3
      Re: Random Problems

      1) You need to either use raw string for your pathnames or use forward
      slashes.
      This is because backslash is an escape character to Python and if you get
      any legal escaped sequence (like \n, \t, etc) it won't work as expected.
      >
      songs = glob.glob(r'C:\ Documents and Settings\Admin\ My
      Documents\LimeW ire\Saved\*.mp3 ')
      >
      or
      >
      songs = glob.glob('C:/Documents and Settings/Admin/My
      Documents/LimeWire/Saved/*.mp3')
      >
      Yes, forward slashes work just fine on windows.
      >
      2) When you have a list (songs) and append another list (asongs) you don't
      get a combined list, you get a list with the last element being the second
      list.
      >
      example:
      >
      >songs = [1,2,3]
      >asongs = [4,5,6]
      >songs.append(a songs)
      >songs
      [1, 2, 3, [4, 5, 6]]
      >>
      >
      What you wanted was songs.extend(as ongs). BTW-Inserting a couple of print
      statements would have shown you this problem pretty quickly.
      >
      -Larry
      >
      Thanks for the speedy responce, it really helped



      -- Posted on news://freenews.netfront.net - Complaints to news@netfront.n et --

      Comment

      • Hrvoje Niksic

        #4
        Re: Random Problems

        "Lanny" <lanny@freshell s.chwrites:
        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. Heres my code:
        >
        import random
        import os
        import glob
        If you need recursive traversal of directories, try os.walk. For
        example:

        mp3s = []
        for root, subdirs, files in os.walk(r'C:\Do cuments and Settings\Admin\ My Documents\Downl oads'):
        for f in files:
        if f.endswith('.mp 3'):
        mp3s.append(os. path.join(root, f))
        # mp3s is now a list of mp3 files under
        # C:\Documents and Settings\Admin\ My Documents\Downl oads

        Comment

        • Cousin Stanley

          #5
          Re: Random Problems


          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.appen d( 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

          Comment

          • Cousin Stanley

            #6
            Re: Random Problems

            list_songs = glob.glob( "'%s/%s'" % ( base_dir , this_dir )
            Missed a closing paren ....

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

            Still .... NOT Tested


            --
            Stanley C. Kitching
            Human Being
            Phoenix, Arizona

            Comment

            • Cousin Stanley

              #7
              Re: Random Problems

              >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.
              >Heres my code:
              >>
              >import random
              >import os
              >import glob
              >>
              >songs = glob.glob('C:\D ocuments and Settings\Admin\ My
              >Documents\Lime Wire\Saved\*.mp 3')
              >>
              >asongs = glob.glob('C:\D ocuments and Settings\Admin\ My
              From: Edwin.Madari@ve rizonwireless.c om
              To: "Cousin Stanley" <cousinstanley@ gmail.com>,
              python-list@python.org
              Date: Yesterday 07:28:18 pm
              use songs.extend( asongs ) # append is for single item
              # - where ever it might be.
              >>l1 = range(5)        
              >>l2 = range(5,10)
              >>l1
              [0, 1, 2, 3, 4]
              >>l2
              [5, 6, 7, 8, 9]
              >>l1.extend(l 2)
              >>l1
              [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
              >>>
              And ....

              Thanks to Edwin Madari for reminding me about
              the extend method available for lists which
              I had totally forgotten about ....


              --
              Stanley C. Kitching
              Human Being
              Phoenix, Arizona

              Comment

              Working...